{% block contents %} 以变量 Django 模板继承为条件
{% block contents %} conditional on a variable Django template inheritance
我正在使用 Django 的模板继承自一个名为 base.html 的基本文件。
我的所有应用程序,除了一个,都要求该文件中存在 {% block contents %} {% endblock %},这是有问题的。
我正在设法使包含 {% block contents %} {% endblock %} 以变量为条件。
到目前为止我在 base.html 中尝试过的内容:
{% if some_variable %}
{% block contents %} {% endblock %}
{% endif %}
但这似乎不起作用。
我也试过:
{% if some_variable %}
{% with 'base_block_content.html' as path %}
{% include path %}
{% endwith %}
{% endif %}
base_block_content.html 就是:
'''
{% block contents %} {% endblock %}
'''
但这也不管用。
我唯一的其他选择是为一个应用程序编写一个完全独立的 base.html,这不太符合 'DRY' 概念。
任何人都可以建议这样做的方法吗?
{% block dynamic-block %}
{% if some_variable %}
{% include 'path/index.html' %}
{% endif %}
{% endblock dynamic-lock %}
我正在使用 Django 的模板继承自一个名为 base.html 的基本文件。
我的所有应用程序,除了一个,都要求该文件中存在 {% block contents %} {% endblock %},这是有问题的。
我正在设法使包含 {% block contents %} {% endblock %} 以变量为条件。
到目前为止我在 base.html 中尝试过的内容:
{% if some_variable %}
{% block contents %} {% endblock %}
{% endif %}
但这似乎不起作用。
我也试过:
{% if some_variable %}
{% with 'base_block_content.html' as path %}
{% include path %}
{% endwith %}
{% endif %}
base_block_content.html 就是:
''' {% block contents %} {% endblock %} '''
但这也不管用。
我唯一的其他选择是为一个应用程序编写一个完全独立的 base.html,这不太符合 'DRY' 概念。
任何人都可以建议这样做的方法吗?
{% block dynamic-block %}
{% if some_variable %}
{% include 'path/index.html' %}
{% endif %}
{% endblock dynamic-lock %}