2011-12-15 23 views
38

どのようにしてDjangoテンプレートシステム内に変数を割り当てることができますか? Restaurantを想定しDjango:テンプレートに変数を代入する

はモデルです:

{% 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 %} 
:あなたはテンプレートタグ「と」内のフィルタを使用することができます
関連する問題