0
0
DjangoHow-ToBeginner · 3 min read

How to Use Template Variables in Django: Simple Guide

In Django, you pass data from your view to the template using a context dictionary, where keys become template variables. Inside the template, you use double curly braces like {{ variable_name }} to display these variables dynamically.
📐

Syntax

Template variables in Django are placeholders for data passed from views. You use double curly braces {{ variable_name }} inside your HTML template to show the value of variable_name.

In your view, you send data as a dictionary called context. The keys in this dictionary become the variable names in the template.

django
{% raw %}
# views.py
from django.shortcuts import render

def my_view(request):
    context = {'name': 'Alice', 'age': 30}
    return render(request, 'my_template.html', context)

# my_template.html
<p>Hello, {{ name }}!</p>
<p>You are {{ age }} years old.</p>
{% endraw %}
Output
<p>Hello, Alice!</p> <p>You are 30 years old.</p>
💻

Example

This example shows how to pass variables from a Django view to a template and display them. The view sends a dictionary with keys title and items. The template uses these variables to render a heading and a list.

django
{% raw %}
# views.py
from django.shortcuts import render

def shopping_list(request):
    context = {
        'title': 'My Shopping List',
        'items': ['Apples', 'Bread', 'Milk']
    }
    return render(request, 'shopping.html', context)

# shopping.html
<h1>{{ title }}</h1>
<ul>
  {% for item in items %}
    <li>{{ item }}</li>
  {% endfor %}
</ul>
{% endraw %}
Output
<h1>My Shopping List</h1> <ul> <li>Apples</li> <li>Bread</li> <li>Milk</li> </ul>
⚠️

Common Pitfalls

One common mistake is forgetting to pass the context dictionary from the view, so the template variables are empty or cause errors.

Another is using incorrect variable names in the template that don't match the keys in the context.

Also, trying to use Python code directly in templates instead of Django template syntax causes errors.

django
{% raw %}
# Wrong: missing context
from django.shortcuts import render

def wrong_view(request):
    return render(request, 'template.html')  # No context passed

# Right: pass context
def right_view(request):
    context = {'message': 'Hello!'}
    return render(request, 'template.html', context)

# Template usage
<p>{{ message }}</p>
{% endraw %}
Output
<p>Hello!</p>
📊

Quick Reference

  • Pass data from view to template using a context dictionary.
  • Use {{ variable_name }} to display variables in templates.
  • Use template tags like {% for %} to loop over lists.
  • Always match variable names exactly between view and template.
  • Templates cannot run Python code directly; use Django template syntax.

Key Takeaways

Pass variables from your Django view to templates using a context dictionary.
Use double curly braces {{ variable }} in templates to display passed data.
Ensure variable names in templates match keys in the context dictionary exactly.
Avoid putting Python code directly in templates; use Django template syntax instead.
Use template tags like {% for %} to work with lists or loops in templates.