Django Template Variables 🎯

beginner
7 min

Django Template Variables 🎯

Welcome back to CodeYourCraft! Today, we're going to dive into a fascinating topic - Django Template Variables. By the end of this lesson, you'll have a solid understanding of how to use and manipulate variables in your Django templates. Let's get started! πŸ“

What are Template Variables?

Template variables are a way to include dynamic content within your Django templates. They allow you to display data fetched from your Django application, making your templates more interactive and engaging. πŸ’‘

Accessing Data in Templates

Django fetches data from your views and passes it to the templates as context. You can access this data using template variables. Here's an example of how to display a simple variable:

python
from django.http import HttpResponse from django.shortcuts import render def my_view(request): my_variable = "Hello, World!" return render(request, 'template.html', {'my_variable': my_variable})

In the above example, we've created a simple view that renders a template named template.html. We've also defined a variable my_variable and passed it to the template as part of the context.

Now, let's display this variable in our template:

html
<!-- template.html --> <html> <body> <h1>{{ my_variable }}</h1> </body> </html>

In the template, we use double curly braces {{ }} to enclose the template variable. When Django renders this template, it replaces {{ my_variable }} with the value of the variable we passed from the view.

Quiz Time! πŸ’‘

Quick Quiz
Question 1 of 1

What does `{{ my_variable }}` represent in a Django template?

Advanced Template Variables

Django also supports more complex template variables, such as loops and filters. Let's explore these concepts with an example:

python
from django.http import HttpResponse from django.shortcuts import render from django.template import Context def my_view(request): numbers = [1, 2, 3, 4, 5] context = Context({ 'numbers': numbers, }) return render(request, 'template.html', context)

In this example, we've passed a list of numbers to the template. Now, let's loop through this list and display each number in our template:

html
<!-- template.html --> <html> <body> {% for number in numbers %} <p>{{ number }}</p> {% endfor %} </body> </html>

In this template, we use a for loop to iterate over the numbers list. For each iteration, we display the current number using a template variable number.

Quiz Time! πŸ’‘

Quick Quiz
Question 1 of 1

What does `{% for number in numbers %}` do in a Django template?

Filters in Django Templates

Django templates also support filters, which allow you to manipulate data before it's displayed. For example, let's convert all numbers in our list to squares:

html
<!-- template.html --> <html> <body> {% for number in numbers %} <p>{{ number|add:"2"|int }}^2 = {{ number|add:"2"|int|squared }}</p> {% endfor %} </body> </html>

In this template, we use filters add, int, and squared to add 2 to each number, convert the result to an integer, and finally square the number.

Quiz Time! πŸ’‘

Quick Quiz
Question 1 of 1

What does `{{ number|add:"2"|int|squared }}` do in a Django template?

Wrapping Up

Congratulations! You've learned about Django template variables, loops, and filters. These concepts form the backbone of dynamic web development with Django.

In the next lesson, we'll dive deeper into Django templates and explore more advanced features like template inheritance and custom filters. Until then, keep coding and learning! βœ…

Remember, the key to mastering Django is practice, so make sure to experiment with these concepts in your own projects. Happy coding! πŸš€