Django Tutorial
Django Create A Complete Project
Django Template
Django Database Query
Django Form
Django Authentication and Permission Management
Django unittest
Django Advanced
In this tutorial, we'll cover some of the other common template tags available in Django. Template tags are enclosed in {% %}
and are used to add logic to your templates.
{% for ... %}
: Loop over a sequence (e.g., a list, tuple, or queryset).{% if ... %}
: Evaluate a condition and display content based on the result.{% include ... %}
: Include another template within the current template.{% with ... %}
: Create an alias for a variable or an expression.{% comment ... %}
: Add a comment that won't be displayed in the final output.{% block ... %}
and {% extends ... %}
: Template inheritance and content blocks.{% for ... %}
Loop over a sequence of items using the {% for ... %}
tag:
<ul> {% for book in books %} <li>{{ book.title }} by {{ book.author }}</li> {% endfor %} </ul>
{% if ... %}
Use {% if ... %}
to evaluate a condition and display content based on the result:
{% if user.is_authenticated %} <p>Welcome, {{ user.username }}!</p> {% else %} <p>Please log in to access this content.</p> {% endif %}
{% include ... %}
Include another template within the current template using {% include ... %}
:
{% include 'navigation.html' %}
{% with ... %}
Create an alias for a variable or an expression using {% with ... %}
:
{% with total=cart.items|length %} <p>You have {{ total }} items in your cart.</p> {% endwith %}
{% comment ... %}
Add a comment in your template that won't be displayed in the final output using {% comment ... %}
:
{% comment %} This section is reserved for future updates. {% endcomment %}
{% block ... %}
and {% extends ... %}
Use {% block ... %}
to define content blocks in a base template and {% extends ... %}
to inherit from a base template in a child template.
base.html:
<!DOCTYPE html> <html> <head> <title>{% block title %}Default Title{% endblock %}</title> </head> <body> <div id="content">{% block content %}{% endblock %}</div> </body> </html>
child.html:
{% extends 'base.html' %} {% block title %}Child Page Title{% endblock %} {% block content %} <h1>Welcome to the child page!</h1> {% endblock %}
In this tutorial, we've covered various other common template tags in Django, such as loops, conditionals, including other templates, aliases, comments, and template inheritance. These tags allow you to add logic and structure to your templates, making them more dynamic and flexible.
Django Template Tags Beyond if and for:
if
and for
statements for more dynamic templates.{% with total_price=item.price|add:tax|floatformat:2 %} Total Price: ${{ total_price }} {% endwith %}
Exploring Advanced Django Template Tags:
regroup
tag){% regroup items by category as grouped_items %} {% for category in grouped_items %} {{ category.grouper }} {% for item in category.list %} {{ item.name }} {% endfor %} {% endfor %}
List of Django Template Tags and Their Uses:
{% if user.is_authenticated %} Welcome, {{ user.username }}! {% else %} Please log in. {% endif %} {% for item in items %} {{ item.name }} {% endfor %}
Working with Built-in Django Template Tags:
url
, block
, and filter
for various tasks.url
tag)<a href="{% url 'app_name:view_name' param1=parameter %}">Link</a>
Django Template Tags for Date and Time:
date
tag){{ some_date|date:"F j, Y" }}
Dynamic Template Tags in Django Examples:
{% if is_featured %} Featured Item {% else %} Regular Item {% endif %}
Creating Reusable Template Tags in Django:
# custom_tags.py from django import template register = template.Library() @register.simple_tag def my_custom_tag(parameter): # Your logic here return result
Conditional Rendering with Django Template Tags:
{% if user.is_authenticated %} Welcome back, {{ user.username }}! {% else %} Please log in to access the content. {% endif %}
Using Template Tags for String Manipulation in Django:
slice
filter){{ long_text|slice:":50" }}
Django Template Tags for Handling URLs:
url
and reverse
.reverse
tag)<a href="{% url 'app_name:view_name' param1=parameter %}">Link</a>
Pagination with Django Template Tags:
{% for item in items %}
){% for item in items %} {{ item.name }} {% endfor %} {% if items.has_other_pages %} <div class="pagination"> <span class="step-links"> {% if items.has_previous %} <a href="?page=1">« first</a> <a href="?page={{ items.previous_page_number }}">previous</a> {% endif %} <span class="current"> Page {{ items.number }} of {{ items.paginator.num_pages }}. </span> {% if items.has_next %} <a href="?page={{ items.next_page_number }}">next</a> <a href="?page={{ items.paginator.num_pages }}">last »</a> {% endif %} </span> </div> {% endif %}
Template Tags for Including Templates in Django:
include
and block
.include
tag){% include 'common/header.html' %}
Django Template Tags for Form Handling:
form
tag)<form method="post" action="{% url 'app_name:view_name' %}"> {% csrf_token %} {{ form.as_p }} <button type="submit">Submit</button> </form>
Advanced Filtering with Django Template Tags:
filter
tag){% for item in items|filter:some_condition %} {{ item.name }} {% endfor %}
Extending Template Tags in Django:
date
tag)# custom_tags.py from django import template from django.template.defaulttags import register @register.filter def custom_date(value, arg): # Your custom date logic here return result
Template Tags for Handling Images in Django:
img
tag)<img src="{{ image.url }}" alt="{{ image.description }}">
Django Template Tags for Content Formatting:
linebreaks
, truncatewords
, etc.linebreaks
tag){{ text|linebreaks }}