如果我转到索引或公告页面,我将分别使用“index”或“announcements”page_name 发送到模板 embed_text。它应该将事件类设置为导航栏中的当前链接。 As shown in the picture (movies is my index page)
这是我在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 %}
它是base.html模板中的代码扩展到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>
因此,每次两个表达式都返回 True 并将两个链接设置为事件状态时,您可以在图片中看到它。我知道,我遗漏了一些细节。请帮忙解决一下。
最佳答案
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 %}
关于python - Django 模板每次返回 True 时都会刺痛比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43035642/