[英]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.