An if tag conditionally renders a block in a Django template. The paired {% if %} and {% endif %} tags evaluate a condition and include the enclosed output only when that condition is true. section 3.2 section 3.5.1
A simple variable condition uses Python-like truthiness: zero, False, empty containers, and missing context variables hide the block; other values show it. section 3.5.1
Use the tag for increasingly specific template decisions:
- Compare variables or values with ==, !=, <, >, <=, >=, in, not in, is, and is not. section 3.5.1
- Combine conditions with and, or, and not; and takes precedence over or, following Python’s ordering. section 3.5.1
- Add {% elif %} for additional tests and {% else %} for the fallback branch. section 3.5.1
Showing a RiffMates welcome heading
The template displays the heading only when first_page is truthy: {% if first_page %} <h1>Welcome to <i>RiffMates</i></h1> {% endif %} A missing first_page value behaves like False, so the heading is omitted rather than causing this block to render. section 3.5.1
{% if first_page %}
<h1> Welcome to <i>RiffMates</i> </h1>
{% endif %}
Inspect the complete conditional listing for the RiffMates welcome heading. section 3.5.1
