Py / Django:在模板中设置变量

时间:2016-01-26 00:42:45

标签: python django

我尝试使用python / Django创建月历视图。 是否可以在{%%}段中设置变量?

我想这样:

  • 1月:
  • 01.01.2016测试条目
  • 06.01.2016 on other entry
  • 25.01.2016去年1月入境
  • 2月:
  • 03.02.2016 hello

新月开始时会生成最高月份名称。 这样的事情可能吗?

{% if entry.month == lastmonth %} 
// show month name
{% lastmonth = entry.month %} 

这是一个for结构?

1 个答案:

答案 0 :(得分:3)

您可以使用{% with %}模板标记在模板中设置变量。有关详细信息,请参阅此处:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#with

在您的情况下,您可以执行以下操作:

{% for item in collection %}
    {% if item-satisfies-condition %}
        {% with variable=some-value %}
            stuffs...
        {% endwith %}
    {% endif %}
{% endfor %}