1

withDjango テンプレートでは、タグを使用して新しい名前付き変数をコンテキストに導入できます。

{% with total=business.employees.count %}
    {{ total }} employee{{ total|pluralize }}
{% endwith %}

再利用可能なテンプレートを含めることができるように、変数の名前を変更するためによく使用しています。

{% with user_list=request.user.friends %}
    {% include '_user_list.html' %}
{% endwith %}

withタグを使用せずにコンテキストに新しい変数を導入する方法はありますか? 私は、set一度呼び出してからタグ{% set user_list=request.user.friends %}で閉じる必要がないようなタグを想像しています{% endset %}。これは{% trans "This is the title" as the_title %}in 関数に似たものです。

これを行う方法がない場合、これが良い/ pythonic / djangoのやり方ではない理由は何ですか?

4

1 に答える 1