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: Templates in Django

Capítulo 85

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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.

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

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.

In Django, templates are responsible for handling the presentation layer of the web application. They allow developers to dynamically write HTML by incorporating Python variables and special tags that manage flow control, such as loops or conditional statements. This design adheres to the separation of concerns principle, keeping presentation and processing logic distinct, which enhances code maintainability.

Next chapter

Templates in Django: URLs in Django

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