18.5. Templates in Django

One of the key features of Django is its ability to manage templates efficiently and effectively. Django's templating system is designed so that developers can dynamically write HTML, allowing data to be presented in an elegant and personalized way. This chapter will explore in detail how templates in Django work and how you can use them to create powerful systems with Python and Django.

What are templates?

In simple terms, a template is a file that serves as a template for creating other documents. In the context of web development, a template is an HTML file that contains placeholders for data that will be dynamically populated. These placeholders are filled with data that is passed to the template by the backend system, in this case Django.

How do templates work in Django?

Django uses a text-based templating system that allows you to insert Python code directly into your HTML files. This is done using a special syntax that allows you to insert variables and run loops and conditionals directly in your HTML. This allows you to create dynamic web pages that can be customized for each user.

For example, if you are creating a blog, you can use a template to create the page for each post. The template can include placeholders for the post title, post content, and publication date. When a user visits a post page, Django will fill these placeholders with data from the specific post the user is viewing.

Using templates in Django

To use a template in Django, you first need to create a template file. This is an HTML file that includes Django's special syntax for placeholders and template logic. You can create a template file in any text editor and save it with the .html extension.

Once you have a template file, you can use it in your Django views. A view is a Python function that takes a web request, processes it, and returns a response. In Django, you can use a view to render a template and populate it with data.

To do this, you can use Django's render() function. This function takes three arguments: the request, the path to the template file, and a dictionary of data that will be used to fill in placeholders in the template. The render() function will then create an HTTP response that includes the HTML generated by the template.

Example of using templates

Suppose you are creating a blog and you have a template for each post page. The template might look something like this:

<h1>{{ title }}</h1>
<p>Published on {{ date }}</p>
<p>{{ content }}</p>

In your view, you can use the render() function to fill this template with data from a specific post:

def post_view(request, post_id):
    post = get_object_or_404(Post, pk=post_id)
    return render(request, 'post.html', {'title': post.title, 'date': post.date, 'content': post.content})

With this code, when a user visits a post page, Django will populate the template with the title, date, and content of the post. This allows you to create dynamic web pages that are personalized for each user.

Conclusion

Django's templating system is a powerful tool that allows you to create dynamic, personalized web pages. By understanding how templates work and how to use them in your views, you can build complex systems with Python and Django efficiently and effectively.

Now answer the exercise about the content:

What is a template in the context of web development with 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:

107Templates 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