Django is a Python web development framework that follows the MVC (Model-View-Controller) design pattern. It is known for its efficiency and flexibility, allowing developers to build complex web applications with less code and effort. One of the most important features of Django is the template system, which allows the separation between programming logic and data presentation.

What are templates?

Templates are files that contain HTML code mixed with a special Django markup language. This markup language allows you to embed variables, conditionals, loops, and other programming elements within HTML. When a template is rendered, Django replaces the tags with the corresponding content, generating the final HTML that will be sent to the browser.

How do templates work in Django?

In Django, each view is responsible for processing an HTTP request and generating a response. This response is usually an HTML document, which is created from a template. The view gets the necessary data from the model, passes that data to the template, and Django takes care of rendering the template with that data.

Django templates are organized in a hierarchical system. Each template can extend a base template, inheriting all of its content. This allows you to create a common structure for all pages on the site, avoiding code repetition. For example, we might have a base template that contains the header and footer for the site, and each page on the site extends that base template, adding only content specific to that page.

How to create a template in Django?

To create a template in Django, we first need to create a directory called 'templates' inside our application folder. Inside that directory, we create a file with the .html extension for each template we want to create.

The content of a template is basically HTML, but we can use Django's markup language to insert variables, conditionals, loops, and other programming elements. Variables are enclosed in double braces {{ }} and represent data that is passed by the view. Conditionals and loops are inserted between braces and percentages {% %} and allow you to control the flow of execution of the template.

For example, to display a list of products, we could have a template like this:

<html>
<body>
<h1>Product List</h1>
<ul>
{% for product in products %}
  <li>{{ product.name }}: {{ product.price }}</li>
{% endfor %}
</ul>
</body>
</html>

In this example, 'products' is a variable that must be passed by the view and 'product' is a variable that represents each product in the list. The for loop loops through the list of products and for each product, inserts a list item with the product name and price.

Conclusion

Django's templating system is a powerful tool that lets you create dynamic, reusable user interfaces. With it, we can separate the programming logic from the data presentation, making the code more organized and easier to maintain. In addition, Django offers many other features that facilitate the development of web applications, such as an ORM (Object-Relational Mapping) for database access, a user authentication system, an administration system and much more. /p>

Now answer the exercise about the content:

What is the role of the template system in Django?

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

You missed! Try again.

Article image Templates in Django: Development Environment Setup

Next page of the Free Ebook:

103Templates in Django: Development Environment Setup

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