简体   繁体   English

基于Django WagtailCMS SITE_ID的显示

[英]Display based on Django WagtailCMS SITE_ID

How can I create an if block to display one of my sidemenu's based on the WagtailCMS SITE_ID? 如何基于WagtailCMS SITE_ID创建一个if块来显示我的一个侧菜单?

Tried this, but it doesn't work 尝试了这个,但是没有用

{% if settings.SITE_ID == 1 %}
   {% include 'includes/_home-sidebar-left.html' %}
{% else %}
   {% include 'includes/_home-sidebar.html' %}
{% endif }

Assuming this is a page template, you can access the current site through the page object with page.get_site() . 假设这是一个页面模板,则可以使用page.get_site()通过页面对象访问当前站点。

That being said, you'll end up with magic strings/numbers (for checking the site ID or name) in your templates. 话虽这么说,您最终会在模板中使用魔术字符串/数字(用于检查站点ID或名称)。 One way to get around that would be to use the wagtail.contrib.settings module. 解决该问题的一种方法是使用wagtail.contrib.settings模块。

After setting up the module correctly, create a settings object (which will appear in the admin) in myapp/wagtail_hooks.py : 正确设置模块后,在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,
    )

And use the settings in the templates myapp/templates/myapp/mytemplate.html 并使用模板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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM