2017-02-07 37 views
1

如何在base.html中的Django CMS中使用條件來檢測頁面是否爲主頁並向body標籤添加唯一類?我寧願不重複基地,只是添加一個類,以便我可以在主頁上以不同方式處理一些樣式。Django CMS有條件

回答

1

這取決於你如何構建你的網頁。

我選擇將頁面創建爲「主頁」頁面的子項,因此請使用類似於頁面標題的內容;

{% if request.current_page.get_ancestors|length <= 1 %} 
    <h1>{{ request.current_page.get_page_title }}</h1> 
{% else %} 
    {% for ance in request.current_page.get_ancestors %} 
     {% if ance.depth == 2 %} 
      <h1>{{ ance.get_page_title }}</h1> 
     {% endif %} 
    {% endfor %} 
{% endif %} 

所以你可以做一些類似的事情;

<body class="{% if request.current_page.get_ancestors|length <= 1 %}base{% endif %}"> 
+0

這正是我所需要的。謝謝! –

+0

@DebbieGray沒問題,高興幫忙:) –