Locked lesson.
About this lesson
In this lesson, we'll discuss how Tags work and how to use them for logic and list functions within your Django code, e.g. for, if, else.
Exercise files
Download this lesson’s related exercise files.
Tags (for, if, else).docx57.3 KB Tags (for, if, else) - Solution.docx
58.1 KB
Quick reference
Tags (for, if, else)
Tags allow us to use if/else statements and for loops on a web page using the Django Template Language.
When to use
Use them whenever you need to do some logic (if/else statements) or loop through data on a web page.
Instructions
To use an if/else tag:
{% if variable == "something" %}
do something
{% else %}
do something else
{% endif %}
To use a for loop:
{% for name in names %}
print out: {{ name }} <br/>
{% endfor %}
Hints & tips
- Tags allow us to do if/else statements and for loops on a web page.
- Don't forget to close your tags!
Lesson notes are only available for subscribers.