Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django奇怪模板条件呈现_Python_Django - Fatal编程技术网

Python Django奇怪模板条件呈现

Python Django奇怪模板条件呈现,python,django,Python,Django,这是我的django模板文件: {% if chart %} {% block chart_content %} // some for loop {% endblock %} {% else %} {% block content %} {% endblock %} {% endif %} 下面是我的“base.html”: {%block content%} {%endblock%} {%block chart_content%} {%en

这是我的django模板文件:

{% if chart %}
    {% block chart_content %}
       // some for loop
    {% endblock %}
{% else %}
    {% block content %}

    {% endblock %}
{% endif %}
下面是我的“base.html”:


{%block content%}
{%endblock%}
{%block chart_content%}
{%endblock%}
呈现上述模板时,“if”和“else”内容都会显示在页面中。换句话说,“如果”和“其他”都要被评估。有人能告诉我问题出在哪里吗?

来自:

不能像环绕块一样环绕控制流标记。您的问题是,使用子模板的块数据定义只是因为它在那里

可以通过将if标记放置在块数据中来修复它。如果要在列表为空时继承父级内容,请添加扩展到{{block.super}的else大小写。

来自:

不能像环绕块一样环绕控制流标记。您的问题是,使用子模板的块数据定义只是因为它在那里

可以通过将if标记放置在块数据中来修复它。如果要在列表为空时继承父级的内容,请添加一个扩展到{{block.super}}的else大小写。

在您确定并使用的“base.htm”模板中,默认情况下,您会看到这些块的内容,并且在继承的模板上,您只能覆盖在父级模板中确定的块的内容,不能设置“显示”或“消失”块,可以通过重新确定没有内容的块来消失继承的块:

{# base.html #}
{% block x %}The X Block{% endblock %}
在以下方面:

{# index.html #}
{% extends "base.html" %}
{% block x %}{% endblock %}
在您确定并使用的“base.htm”模板中,默认情况下,您会看到这些块的内容,并且在继承的模板上,您只能覆盖在父模板中确定的块的内容,您不能设置“显示”或“消失”块,您可以通过重新确定没有内容的块来消失继承的块:

{# base.html #}
{% block x %}The X Block{% endblock %}
在以下方面:

{# index.html #}
{% extends "base.html" %}
{% block x %}{% endblock %}

关于模板规则,其他答案是正确的。然而,如果你只是想修正你的结果,也许像这样的事情可以解决问题

{% block chart_content %}
    {% if chart %}
           // some for loop
    {% endif %}
{% endblock %}

{% block content %}

any conditionals you see fit on 
the contents of this block
for example:

{% if not chart %}
   it did see like you wanted
   to have something here
   if chart was empty
{% else %}

{% endif %}


{% endblock %}

关于模板规则,其他答案是正确的。然而,如果你只是想修正你的结果,也许像这样的事情可以解决问题

{% block chart_content %}
    {% if chart %}
           // some for loop
    {% endif %}
{% endblock %}

{% block content %}

any conditionals you see fit on 
the contents of this block
for example:

{% if not chart %}
   it did see like you wanted
   to have something here
   if chart was empty
{% else %}

{% endif %}


{% endblock %}