2017-03-26 97 views
1

如果我進入索引或通告頁面,我將分別通過'index'或'announcementments'page_name發送到模板embed_text。它應該將活動類設置爲導航欄中的當前鏈接。 As shown in the picture (movies is my index page)Django模板每次都會返回true

這是我在index.html的模板

{% if embed_text.page_name == 'index' %} 
    {% block movies_active %}active{% endblock %} 
{% endif %} 
{% if embed_text.page_name == 'announcements' %} 
    {% block announcements_active %}active{% endblock %} 
{% endif %} 

代碼,它是在延伸到index.html的

<li class="{% block movies_active %}{% endblock %}"><a href="{% url 'movies:index' %}"> Movies</a></li> 
<li class="{% block announcements_active %}{% endblock %}"><a href="{% url 'movies:announcements' %}"> Announcemets</a></li> 

所以base.html文件模板代碼,每次都的表達式返回True並將這兩個鏈接設置爲活動狀態,您可以在圖片中看到它。我知道,我錯過了一些細節。請幫忙解決它。

回答

1

block標記不受if標記的影響。你可以試試這個。

{% block movies_active %}{% if embed_text.page_name == 'index' %}active{% endif %}{% endblock %} 
{% block announcements_active %}{% if embed_text.page_name == 'announcements' %}active{% endif %}{% endblock %} 
+0

現在,它的工作。謝謝 – Ujinjinjin