Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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
解决在django模板中设置变量的问题_Django_Templates - Fatal编程技术网

解决在django模板中设置变量的问题

解决在django模板中设置变量的问题,django,templates,Django,Templates,我有一段代码,我在twig中使用它来根据同一日期显示部分 我试图在Django中使用相同的代码,但无法在其模板系统中设置变量。做这件事的明智方法是什么?正确的方法?人们如何解决这样的问题 {% set date = "prout" %} {% for article in articles %} {% if article.date != date %} {% if date != "prout" %} </ul> </se

我有一段代码,我在twig中使用它来根据同一日期显示部分

我试图在Django中使用相同的代码,但无法在其模板系统中设置变量。做这件事的明智方法是什么?正确的方法?人们如何解决这样的问题

{% set date = "prout" %}

{% for article in articles %}
  {% if article.date != date %}
     {% if date != "prout" %} 
          </ul>
        </section>
     {% endif %}

     {% set date = article.date %}

     <section class="row">
        <h2>{{ article.date }}</h2>
        <ul>
            <li>+ {{ article.titre }}</li>
  {% else %}
      <li>+ {{ article.titre }}</li>
  {% endif %}
{% endfor %}
 </ul>
 </section>
{%set date=“prout”%}
{文章%中的文章为%s}
{%if article.date!=date%}
{%if日期!=“prout”%}

{%endif%}
{%set date=article.date%}
{{article.date}
  • +{{article.titre}
  • {%else%}
  • +{{article.titre}
  • {%endif%} {%endfor%}

与“设置”变量更接近的概念是带有标签的
。引述:

以更简单的名称缓存复杂变量。这是有用的 当访问“昂贵”方法时(例如,点击 数据库)多次

例如:

{% with total=business.employees.count %}
    {{ total }} employee{{ total|pluralize }}
{% endwith %}

您应该在视图中而不是在模板中执行逻辑。同样在django你可以做{%用'prout'作为date%}我对django很陌生,我真的不知道该怎么做。在视图或模型中?可能与