Django templates are a crucial part of the framework's architecture, allowing developers to separate the presentation logic from the business logic. They provide a powerful way to generate dynamic HTML content, making it easier to create responsive and interactive web applications.
Django's template language is designed to strike a balance between power and simplicity. It's expressive enough to handle complex rendering tasks while remaining accessible to designers and developers who may not be Python experts.
To output a variable in a template, use double curly braces:
<p>Welcome, {{ user.username }}!</p>
Tags allow you to perform programming logic within your templates. They are enclosed in {% %} brackets:
{% if user.is_authenticated %} <p>Hello, {{ user.username }}!</p> {% else %} <p>Please log in.</p> {% endif %}
Filters modify the output of variables. They are applied using the pipe (|) character:
<p>Joined: {{ user.date_joined|date:"F j, Y" }}</p>
One of the most powerful features of Django's template system is inheritance. It allows you to build a base "skeleton" template that contains all the common elements of your site and defines blocks that child templates can override.
<!DOCTYPE html> <html lang="en"> <head> <title>{% block title %}My Website{% endblock %}</title> </head> <body> <div id="content"> {% block content %}{% endblock %} </div> </body> </html>
{% extends "base.html" %} {% block title %}Welcome to My Website{% endblock %} {% block content %} <h1>Welcome!</h1> <p>This is the homepage.</p> {% endblock %}
Sometimes, you need more complex logic in your templates. Django allows you to create custom template tags:
# In your_app/templatetags/custom_tags.py from django import template register = template.Library() @register.simple_tag def multiply(a, b): return a * b
Use it in your template:
{% load custom_tags %} <p>Result: {% multiply 4 5 %}</p>
For reusable components, use the include tag:
{% include "navbar.html" %}
Context processors let you add variables to the template context for all templates:
# In your settings.py TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'OPTIONS': { 'context_processors': [ 'your_app.context_processors.global_settings', ], }, }, ] # In your_app/context_processors.py def global_settings(request): return { 'SITE_NAME': 'My Awesome Website', }
Now, SITE_NAME
is available in all templates:
<h1>Welcome to {{ SITE_NAME }}</h1>
{# This section handles user authentication #} {% if user.is_authenticated %} ... {% endif %}
Django's template system is a powerful tool for creating dynamic, maintainable web applications. By leveraging features like inheritance, custom tags, and context processors, you can create elegant, efficient templates that separate concerns and promote code reuse. As you continue to work with Django, you'll discover even more ways to harness the full potential of its templating capabilities.
22/11/2024 | Python
17/11/2024 | Python
06/10/2024 | Python
26/10/2024 | Python
25/09/2024 | Python
14/11/2024 | Python
06/10/2024 | Python
15/11/2024 | Python
17/11/2024 | Python
14/11/2024 | Python
26/10/2024 | Python
14/11/2024 | Python