2011-12-15 106 views
38

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

Restaurant假设是一个型号:

{% restaurant_id as restaurant.id %}{{ restaurant_id as restaurant.id }}不工作。

回答

71

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

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

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

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