SoFunction
Updated on 2025-03-03

Detailed explanation of templates usage examples in Django

Django's template system is a powerful tool for rendering dynamic data into HTML pages. The following are the detailed usage of the Django template system:

Basic concepts of templates

Django templates use a special syntax to insert variables, labels, and filters.

Create a template

Create a template directory: Create a name called in your Django apptemplatesdirectory.

Write template files:existtemplatesCreate HTML files in the directory, the file extension is usually.html

Template inheritance

Django templates support inheritance, which means you can create a base template and allow other templates to inherit it.

<!--  -->
<html>
<head>
    <title>{% block title %}My Site{% endblock %}</title>
</head>
<body>
    {% block content %}
    {% endblock %}
</body>
</html>
<!--  -->
{% extends "" %}
{% block title %}Home Page{% endblock %}
{% block content %}
    <h1>Welcome to my site!</h1>
{% endblock %}

variable

In the template, variables use{{ variable }}express.

<p>Hello, {{ name }}!</p>

Label

Django templates provide many built-in tags, e.g.forcycle,ifConditional statements, etc.

{% if user.is_authenticated %}
    <p>Welcome, {{  }}!</p>
{% else %}
    <p>Welcome, guest!</p>
{% endif %}

Filter

Filters can be used to modify the output of a variable. Filter usage|symbol.

{{ article.pub_date|date:"Y-m-d" }}

Custom filters

You can create your own filters and use them in your templates.

# Create a custom filter in the template tags directory of the applicationfrom django import template
register = ()
@(name='my_filter')
def my_filter(value):
    return ()

Then use in the template:

{{ my_variable|my_filter }}

Template tags

Template tags are used to perform specific logic.

{% for article in articles %}
    <p>{{  }}</p>
{% endfor %}

URL in template

In the template, you can useurlTags to generate URLs.

<a href="{% url 'view_name' arg %}" rel="external nofollow"  rel="external nofollow" >Link</a>

Comments

In the template, you can use{# ... #}to add comments.

{# This is a comment #}

Static files

In the template, you can use{% static %}Tags to reference static files.

<link rel="stylesheet" href="{% static 'css/' %}" rel="external nofollow"  rel="external nofollow" >

Template context

In the view, you can pass a context dictionary to the template.

from  import render
def my_view(request):
    return render(request, 'my_template.html', {'name': 'Kimi'})

Template string

You can use template strings in templates to create dynamic content.

{% with name="Kimi" %}
    <p>Hello, {{ name }}!</p>
{% endwith %}

Forms in templates

Django templates provide form rendering tags, e.g.formTags andform.as_p

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Submit</button>
</form>

Static methods in templates

In the template, you can use{{ .label_tag }}to render the field's label.

Loops in templates

You can use it in templatesforLoop to iterate over the data.

<ul>
    {% for item in items %}
        <li>{{  }}</li>
    {% endfor %}
</ul>

Conditions in the template

You can use it in templatesifTags to execute conditional logic.

{% if user.is_authenticated %}
    <p>Welcome back, {{  }}!</p>
{% else %}
    <p>Please log in.</p>
{% endif %}

Comments in templates

You can use it in templates{# ... #}to add comments.

{# This is a comment #}

Reverse parsing of URLs in templates

You can useurlTags to generate URLs.

<a href="{% url 'view_name' arg %}" rel="external nofollow"  rel="external nofollow" >Link</a>

Static files in templates

You can usestaticTags to reference static files.

<link rel="stylesheet" href="{% static 'css/' %}" rel="external nofollow"  rel="external nofollow" >

Custom labels and filters in templates

You can create custom tags and filters and use them in templates.

Template in template

You can include another template in one template.

{% include "" %}

Escape characters in templates

In the template, you can use{{ variable|escape }}To escape HTML.

Cache in templates

You can use{% cache %}Tags to cache part of the template.

{% cache 500 sidebar %}
    &lt;!-- Cache content --&gt;
{% endcache %}

With these basic usages, you can create dynamic and feature-rich web pages.

This is the end of this article about the detailed explanation of the use of templates in Django. For more related content on using Django templates, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!