【问题标题】:Django: Assigning variables in templateDjango:在模板中分配变量
【发布时间】:2012-01-21 00:31:06
【问题描述】:

如何在 django 模板系统中分配变量?

假设Restaurant 是一个模型:

{% restaurant_id as restaurant.id %}{{ restaurant_id as restaurant.id }} 不起作用。

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    您可以使用with template tag,并像这样分配一个内部模板变量:

    {% with restaurant_id=restaurant.id %}
    ... use restaurant_id in this template section ...
    {% endwith %}
    

    【讨论】:

      【解决方案2】:

      注意:您可以在“with”模板标签中使用过滤器:

      foo is {{ foo }}
      {% with bar=foo|slice'6:9' %}
        bar is {{ bar }}
      {% endwith %}
      

      【讨论】:

        猜你喜欢
        • 2016-07-14
        • 1970-01-01
        • 2012-05-03
        • 2012-07-23
        • 1970-01-01
        • 2012-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多