Django, one of the leading Python web development frameworks, has a powerful and flexible templating system. Django's templating system is an integral part of the framework that makes it easy and efficient to generate dynamic HTML content. Throughout this chapter, we'll explore the core concepts and functionality of templates in Django.

What are Templates?

In web development, a template is a file that serves as a starting point for generating HTML content. Templates typically contain static HTML structures, with placeholders for dynamic content. These placeholders are filled with data when the template is rendered.

Why use Templates?

Using templates in your Django application has several advantages. First, they allow you to separate the business logic from the presentation. This makes your code cleaner and easier to maintain. Second, the templates are reusable. You can create a base layout and reuse it across multiple pages. Finally, Django's templates are written in a simple, easy-to-learn language, making them accessible even to people with no programming experience.

How do Templates work in Django?

Django's templating system uses its own language, called Django Template Language (DTL). DTL is a markup language that you can use to define the structure of your HTML content and insert dynamic data.

Django templates are composed of tags, variables, and filters. Tags are used to create logic in your template, such as loops and conditionals. Variables are used to insert dynamic data into your template. Filters are used to modify the output of your variables.

How to create a Template in Django?

To create a template in Django, you need to create a file with the .html extension in your templates directory. The filename will be the name of your template. Within this file, you can write your HTML as normal, and use the language DTL to insert logic and dynamic data.

Example of a Template in Django:

    
    <!DOCTYPE html>
    <html>
    <head>
        <title>{{ title }}</title>
    </head>
    <body>
        <h1>{{ heading }}</h1>
        {% for item in items %}
            <p>{{ item }}</p>
        {% endfor %}
    </body>
    </html>
    
    

How to render a Template in Django?

To render a template in Django, you need to use the render() function in your view. This function accepts three arguments: the request, the template name and a dictionary containing the data you want to insert into the template.

Example of how to render a Template in Django:

    
    from django.shortcuts import render

    def home(request):
        return render(request, 'home.html', {'title': 'Home', 'heading': 'Welcome!', 'items': ['Item 1', 'Item 2', 'Item 3']} )
    
    

Conclusion

Django's templating system is a powerful tool that lets you create dynamic HTML content efficiently and easily. Through the use of templates, you can separate the business logic from the presentation, making your code cleaner and easier to maintain. Furthermore, the templates are reusable and written in a simple and easy-to-learn language.

Now answer the exercise about the content:

What is a Template in Django and what is its function?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Templates in Django: Introduction to Django 102

Next page of the Free Ebook:

Templates in Django: Introduction to Django

Estimated reading time: 4 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks