【发布时间】:2018-09-12 02:09:47
【问题描述】:
我有以下代码在 Django HTML 模板中工作。但是,它非常重复。这段代码如何简化?
意思是,“如果您不是员工,则可以看到 nav.html。如果您是员工,则只有在这 4 个页面上才能看到 nav.html。”
{% if not request.user.is_staff %}
{% include ‘nav.html’ %}
{% else %}
{% if request.get_full_path == ‘/one/’ %}
{% include ‘nav.html’ %}
{% if request.get_full_path == ‘/two/’ %}
{% include ‘nav.html’ %}
{% if request.get_full_path == ‘/three/’ %}
{% include ‘nav.html’ %}
{% if request.get_full_path == ‘/four/’ %}
{% include ‘nav.html’ %}
{% endif %}
{% endif %}
【问题讨论】:
-
到目前为止你有没有尝试过?您可以检查变量是否在某个列表中。
标签: html django if-statement conditional jinja2