【发布时间】:2012-01-21 00:31:06
【问题描述】:
如何在 django 模板系统中分配变量?
假设Restaurant 是一个模型:
{% restaurant_id as restaurant.id %} 或 {{ restaurant_id as restaurant.id }} 不起作用。
【问题讨论】:
如何在 django 模板系统中分配变量?
假设Restaurant 是一个模型:
{% restaurant_id as restaurant.id %} 或 {{ restaurant_id as restaurant.id }} 不起作用。
【问题讨论】:
您可以使用with template tag,并像这样分配一个内部模板变量:
{% with restaurant_id=restaurant.id %}
... use restaurant_id in this template section ...
{% endwith %}
【讨论】:
注意:您可以在“with”模板标签中使用过滤器:
foo is {{ foo }}
{% with bar=foo|slice'6:9' %}
bar is {{ bar }}
{% endwith %}
【讨论】: