... var2 var3 %} 上のコードは以下と等価です: {% if var1 %} {{ var1 }} {% elif var2 %} {{ var2 }} {% elif var3 %} {{ var3 }} {% endif %} また、渡された変数がすべて False の場合のフォールバック値として、リテラル文字列を使うこともできます: {% firstof var1 va ... hlete_list %} < li > {{ athlete.name }} </ li > {% endfor %} {% else %} < li > Sorry, no athletes in this list. </ li > {% endif %} </ ul > if {% if %} タグは変数を評価し、その変数が "true" であるとき (すなわち変数が存在し、空ではなく、その値が False ... ength }} {% elif athlete_in_locker_room_list %} Athletes should be out of the locker room soon! {% else %} No athletes. {% endif %} 上の例では、 athlete_list が空でなければ、アスリートの人数を {{ athlete_list|length }} 変数で表示します。 例にもあるよう ... ... .title }} </ li > {% endfor %} </ ul > < p > {% if previous_month %} Previous Month: {{ previous_month | date :"F Y" }} {% endif %} {% if next_month %} Next Month: {{ next_month | date :"F Y" }} {% endif %} </ p > WeekArchiveView class WeekArchiveView ... l > < p > {% if previous_week %} Previous Week: {{ previous_week | date :"W" }} of year {{ previous_week | date :"Y" }} {% endif %} {% if previous_week and next_week %} -- {% endif %} {% if next_week %} Next week: {{ next_week | date :"W" }} of year { ... { next_week | date :"Y" }} {% endif %} </ p > この例では週番号を出力しています。 テンプレートフィルタの date が 'W' フォーマット文字列で計算した週番号は、 str ... ... es %} < ul class = "messages" > {% for message in messages %} < li {% if message.tags %} class = " {{ message.tags }} " {% endif %} > {{ message }} </ li > {% endfor %} </ ul > {% endif %} コンテキストプロセッサーを使っている場合、テンプレートは Reque ... es %} < ul class = "messages" > {% for message in messages %} < li {% if message.tags %} class = " {{ message.tags }} " {% endif %} > {% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %} Important: {% endif %} {{ message }} </ li > {% endfor %} </ u ... l > {% endif %} テンプレート以外では 、 get_messages() を使うことができます: from django.contrib.messages import get_messages storage = ... ... et %} <url> <loc> {{ url.location }} </loc> {% if url.lastmod %} <lastmod> {{ url.lastmod | date :"Y-m-d" }} </lastmod> {% endif %} {% if url.changefreq %} <changefreq> {{ url.changefreq }} </changefreq> {% endif %} {% if url.priority %} <priority> {{ ... url.priority }} </priority> {% endif %} <news:news> {% if url.item.publication_date %} <news:publication_date> {{ url.item.publication_date | date :"Y-m-d" }} ... </news:publication_date> {% endif %} {% if url.item.tags %} <news:keywords> {{ url.item.tags }} </news:keywords> {% endif %} </news:news> </url> {% endfor % ... ... > {% for error in form.subject.errors %} < li >< strong > {{ error | escape }} </ strong ></ li > {% endfor %} </ ol > {% endif %} 非フィールドエラー(および/または form.as_p() のようなヘルパーを使用したときにフォームの先頭にレンダリングされる隠しフィ ... {% if field.help_text %} < p class = "help" id = " {{ field.auto_id }} _helptext" > {{ field.help_text | safe }} </ p > {% endif %} </ div > {% endfor %} {{ field }} の有用な属性は次のとおりです: {{ field.errors }} このフィールドに対応するバリデーショ ... しては特に有用ではありませんが、以下のような条件テストに役立ちます。 {% if field.is_hidden %} {# Do something special #} {% endif %} {{ field.label }} The label of the field, e.g. Contact email . {{ field.label_tag }} フィールドのラベルは、適切なHTMLの ... ... ml" %} {% block content %} {% if form.errors %} < p > Your username and password didn't match. Please try again. </ p > {% endif %} {% if next %} {% if user.is_authenticated %} < p > Your account doesn't have access to this page. To proceed, please lo ... gin with an account that has access. </ p > {% else %} < p > Please login to see this page. </ p > {% endif %} {% endif %} < form method = "post" action = " {% url 'login' %} " > {% csrf_token %} < table > < tr > < td > {{ form.us ... Welcome, {{ user.username }} . Thanks for logging in. </ p > {% else %} < p > Welcome, new user. Please log in. </ p > {% endif %} RequestContext が使用されていない場合、このテンプレートコンテキスト変数は無効となります。 パーミッション 現在ログイン中 ... ... < a href = "?page=1" > « first </ a > < a href = "?page= {{ page_obj.previous_page_number }} " > previous </ a > {% endif %} < span class = "current" > Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }} . </ span > {% if page_obj. ... e_obj.next_page_number }} " > next </ a > < a href = "?page= {{ page_obj.paginator.num_pages }} " > last » </ a > {% endif %} </ span > </ div > Paginator をビュー関数で使用する 以下は、queryset をページ分割するビュー関数内の Paginator の一例です ... ... ルターは、 length と == 演算子を {% if %} タグ内で使用する形式に置き換えられました。例えば {% if value | length == 4 %} … {% endif %} {% if value | length == 4 %} True {% else %} False {% endif %} これは以下の表記に代わるものです: {% if value | length_i ... s : 4 %} … {% endif %} {{ value | length_is : 4 }} django.contrib.auth.hashers.SHA1PasswordHasher , django.contrib.auth.hashers.UnsaltedSHA1Pa ... ... _text %} < div class = "helptext" id = " {{ form.name.auto_id }} _helptext" > {{ form.name.help_text | safe }} </ div > {% endif %} {{ form.name.errors }} {{ form.name }} < div class = "row" > < div class = "col" > {{ form.email.label_tag }} {% if for ... ext %} < div class = "helptext" id = " {{ form.email.auto_id }} _helptext" > {{ form.email.help_text | safe }} </ div > {% endif %} {{ form.email.errors }} {{ form.email }} </ div > < div class = "col" > {{ form.password.label_tag }} {% if form.passwo ... < div class = "helptext" id = " {{ form.password.auto_id }} _helptext" > {{ form.password.help_text | safe }} </ div > {% endif %} {{ form.password.errors }} {{ form.password }} </ div > </ div > </ div > ... </ form > これを、以下のように簡略化できる ... ... ength }} {% elif athlete_in_locker_room_list %} Athletes should be out of the locker room soon! {% else %} No athletes. {% endif %} 上記の場合、 athlete_list が空でなければ、 {{ athlete_list|length }} 変数によって選手の数が表示されます。もし athlete_i ... | length > 1 %} Team: {% for athlete in athlete_list %} ... {% endfor %} {% else %} Athlete: {{ athlete_list.0.name }} {% endif %} 上の例はうまくいきますが、ほとんどのテンプレートフィルタは文字列を返すので、フィルタを使った数学的な比較は通常は期待通り ... ンプレートは title ブロックの内容を常に上書きします: {% if change_title %} {% block title %} Hello! {% endblock title %} {% endif %} 最後に、同じテンプレート内で同じ名前の block タグを複数定義することはできないことに留意してください。この制限は、block ... 2026年10月08日