python - Django 模板每次返回 True 时都会刺痛比较

标签 python html django python-3.x django-templates

如果我转到索引或公告页面,我将分别使用“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/

相关文章:

python - __str__ 没有足够的格式字符串参数

python - Django "Unable to determine the file' s size“tempfile.TemporaryFile 错误

javascript - 从文本 JavaScript 中剥离 HTML

python - 在 django 模型中自动生成席位

json - django 休息 api : JSON parse error - No JSON object could be decoded

python - python如何在评论中产生错误?

python - 将多个文件中的所有列合并为一个(在一行中!)

javascript - 语法错误: missing variable name?

java - 当我使用 BufferedReader 获取 HTML 时,我需要的部分不存在

django - 如何显示 django-admin 用户表中的列 ID?