Tsallaka zuwa abun ciki
Justice Tech School

Tags: if and for

Tags use {% %}. if tests conditions, for loops over lists and empty handles empty lists. forloop.counter gives the position.

Example
<ul>
{% for post in posts %}
  <li>{{ forloop.counter }}. {{ post.title }}</li>
{% empty %}
  <li>No posts yet.</li>
{% endfor %}
</ul>
{% if user.is_authenticated %}Hi {{ user.username }}{% else %}Please log in{% endif %}

Exercise

Close a for loop.

{% for x in xs %}...{%  %}

Quiz Test

What does empty do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.