Free Ebook cover System creation course with Python and Django complete

System creation course with Python and Django complete

New course

176 pages

Templates in Django: Introduction to Django

Capítulo 102

Estimated reading time: 4 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

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.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

The template system in Django allows the creation of dynamic and reusable user interfaces by separating programming logic from data presentation. This is achieved through the use of templates that contain HTML mixed with Django markup language, enabling the integration of variables and control structures. This separation helps maintain cleaner and more organized code, as well as promoting code reusability across different parts of the application.

Next chapter

Templates in Django: Development Environment Setup

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