Django is a framework for developing web applications, written in Python, which follows the MVT (Model-View-Template) architecture model. In this model, the "View" deals with the processing logic, the "Model" deals with the data and the "Template" is responsible for presenting the data to the user.

Templates in Django are a crucial part of the framework, as they allow developers to dynamically write HTML by incorporating variables and flow control logic. Django's template library is powerful and easy to use, offering a flexible way to generate HTML from data.

15.11. Templates in Django: Templates in Django

Templates in Django are written in a language that Django can interpret, allowing the insertion of Python variables and special tags that control the flow logic.

A Django template is basically a text file that defines the structure or layout of an HTML file, with placeholders for the data that will be inserted when the template is rendered.

Django's templating system is built on a "separation of concerns" philosophy. This means that templates are designed to handle exclusively the presentation of data, while processing and data manipulation logic is handled elsewhere in the code.

Using Variables in Templates

Variables in a Django template are represented by {{ variable }}. When the template is rendered, these placeholders are replaced with the actual values ​​of the variables.

For example, if you have a variable called 'title' in your template context, you can use it in your template like this:

<h1>{{ title }}</h1>

When the template is rendered, {{ title }} will be replaced with the actual value of the 'title' variable.

Template Tags

Template tags allow you to add flow control logic to your template. Tags are represented by {% tag %}.

For example, the 'for' tag allows you to iterate over a list of items. If you have a list of 'items' in your template context, you can display them in your template like this:

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

There are many other tags available, including tags for conditional flow control (if, else), including other templates (include), template inheritance (extends), and many others.

Rendering a Template

To render a template, you need to first load it into a Template instance and then call the 'render' method of that instance, passing the template context as an argument.

The template context is a dictionary that maps variable names to their values. When the template is rendered, placeholders for variables are replaced with the corresponding values ​​in context.

Conclusion

Templates in Django are a powerful tool for generating dynamic HTML. They allow you to separate presentation logic from processing logic, resulting in cleaner, more maintainable code.

With practice, you'll find that Django's templating system is flexible and powerful, allowing you to create complex user interfaces with ease.

Now answer the exercise about the content:

What is the role of templates in Django?

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

You missed! Try again.

Article image Templates in Django: URLs in Django

Next page of the Free Ebook:

86Templates in Django: URLs in Django

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text