Basic Django Template Tags: A Complete Guide
For most Django developers, template tags are where backend data first becomes a real user-facing interface. If you learn the basics well, you can build most Django frontends comfortably using these basic tags.
When building pages in Django, you usually pass data from Python views into HTML templates. But raw HTML alone cannot handle logic like loops, conditions, inheritance, links, or static assets.
That's why Django provides us with templatetags.
Django template tags are special instructions written inside templates that allow you to: - Render dynamic content - Loop through data - Use conditional logic - Reuse layouts - Load static files - Generate URLs - Organize templates cleanly
They are one of the core parts of Django’s templating system.
This article covers the most important basic template tags every Django developer should know.
What Are Template Tags?
Template tags use this syntax:
⧉
1 | |
They are different from template variables:
⧉
1 | |
The Difference:
| Syntax | Purpose |
|---|---|
| {{ ... }} | Output a value |
| {% ... %} | Perform template logic |
Example:
⧉
1 2 3 4 5 6 | |
If the variable inside {{}} is a Python dict, you can access its elements using:
⧉
1 | |
If the variable inside {{}} is a Python list, you can access its elements by index using:
⧉
1 | |
Nested elements:
⧉
1 | |
Why Django Uses Template Tags
Django intentionally separates: - Python logic in views/models - Presentation logic in templates
Template tags provide safe, limited logic in HTML without turning templates into full Python scripts.
The Most Important Basic Tags
1.
Conditional rendering.
⧉
1 2 3 4 | |
With Else
⧉
1 2 3 4 5 6 7 8 9 | |
Comparisons
⧉
1 2 3 4 5 | |
Logical Operators
⧉
1 | |
Supports: - and - or - not
2.
Loop through lists, querysets, tuples, etc.
⧉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Loop Variables
Inside loops you can use: - {{ forloop.counter }} -> counter starting at 1 - {{forloop.counter0}} -> counter starting at 0 - {{ forloop.first }} -> first element in the loop - {{ forloop.last }} -> last element in the loop
Example:
⧉
1 2 3 4 5 | |
3. {% block %} and {% extends %}
Used for template inheritance.
Base Template:
⧉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
This let's you reuse the full HTML baseline and content that you want rendered multiple times over.
Typical components inside base.html: - Header - Footer - Navigation - Layout structure
4.
Insert another template.
⧉
1 | |
Great for isolated, yet reusable components: - Navbar - Footer - Cards - Alerts - CTA - Sign ups
5.
Generate URLs dynamically using Django URL names.
Instead of hardcoding:
⧉
1 | |
Use:
⧉
1 | |
With Arguments
⧉
1 | |
Why Important
Never hardcode URLs in your templates. When using url-tags they automatically resolve correctly if URLs change and the templates still work without needing to change them.
6.
Used for CSS, JS, images, etc. -> generally all static content
⧉
1 | |
Then:
⧉
1 | |
Example Image
⧉
1 2 3 4 | |
7.
Critical for forms using POST.
⧉
1 2 3 4 | |
Protects against Cross-Site Request Forgery.
Django requires this tag in POST forms. Otherwise Django will raise an exception.
Want to know more about {% csrf_token %} and how it's implemented properly? csrf_token explained Want to understand the attack this protects against? What is CSRF?
8.
Template comments not visible in HTML source.
⧉
1 2 3 4 5 | |
9.
Store a temporary variable.
⧉
1 2 3 4 5 | |
Useful when repeating expensive expressions or when loading tags with a return value.
10.
Current date/time.
⧉
1 | |
Example:
⧉
1 | |
Example Page:
⧉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Common Mistakes:
Forgetting {% endfor %} or {% endif %}
⧉
1 | |
Will raise an exception!
Must close:
{% endfor %} or {% endif %}
Using {{ }} Instead of {% %}
Wrong:
⧉
1 | |
Correct:
⧉
1 2 3 4 5 | |
Hardcoding URLs
Bad: href="/posts/5/"
Correct:
⧉
1 | |
Forgetting csrf_token
Django will raise an exception
Putting Too Much Logic in Templates
Avoid complex logic in templates.
Do heavy work in: - Views - Models - Services
Templates should stay readable.
Template Tags give Django templates controlled power: - Dynamic pages - Clean layouts - Reusability - Safe rendering - Maintainability
Without tags, templates would be static HTML.
Template Tags are small instructions embedded in HTML that let Django assemble pages dynamically.
Most Important Tags to Memorize: - {% if %}{% endif %} - {% for %}{% endfor %} - {% extends %} - {% block %} - {% include %} - {% url %} - {% load static %} - {% csrf_token %}
Basic Django template tags are intentionally limited - but that limitation is a strength. They keep presentation logic clean while still giving enough power to build dynamic pages efficiently.
If you learn the basics well, you can build most Django frontends comfortably using these basic tags.
For most Django developers, template tags are where backend data first becomes a real user-facing interface.
If basic templatetags are not enough for your project, use your own: Write Your Own Custom Templatetags
Join the Newsletter
Practical insights on Django, backend systems, deployment, architecture, and real-world development — delivered without noise.
Get updates when new guides, learning paths, cheat sheets, and field notes are published.
No spam. Unsubscribe anytime.
There is no third-party involved so don't worry - we won't share your details with anyone.