[英]Display based on Django WagtailCMS SITE_ID
如何基于WagtailCMS SITE_ID创建一个if
块来显示我的一个侧菜单?
尝试了这个,但是没有用
{% if settings.SITE_ID == 1 %}
{% include 'includes/_home-sidebar-left.html' %}
{% else %}
{% include 'includes/_home-sidebar.html' %}
{% endif }
假设这是一个页面模板,则可以使用page.get_site()通过页面对象访问当前站点。
话虽这么说,您最终会在模板中使用魔术字符串/数字(用于检查站点ID或名称)。 解决该问题的一种方法是使用wagtail.contrib.settings模块。
正确设置模块后,在myapp/wagtail_hooks.py
创建一个设置对象(将出现在管理员中):
from wagtail.contrib.settings.models import BaseSetting, register_setting
@register_setting
class LayoutSettings(BaseSetting):
POSITION_LEFT = 'left'
POSITION_RIGHT = 'right'
POSITIONS = (
(POSITION_LEFT, 'Left'),
(POSITION_RIGHT, 'Right'),
)
sidebar_position = models.CharField(
max_length=10,
choices=POSITIONS,
default=POSITION_LEFT,
)
并使用模板myapp/templates/myapp/mytemplate.html
{% if settings.myapp.LayoutSettings.sidebar_position == 'left' %}
{% include 'includes/_home-sidebar-left.html' %}
{% else %}
{% include 'includes/_home-sidebar.html' %}
{% endif }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.