0
0
Djangoframework~5 mins

Template variables with double braces in Django

Choose your learning style9 modes available
Introduction

Template variables with double braces let you show data inside your web pages easily. They help you put dynamic content in HTML without writing complex code.

When you want to display user names or messages on a webpage.
When you need to show data from your database inside HTML.
When you want to update parts of a page without changing the whole layout.
When you want to keep your HTML clean and separate from Python code.
Syntax
Django
{{ variable_name }}
Use double curly braces to insert variables inside Django templates.
The variable inside must be passed from your Django view to the template.
Examples
Shows the value of user_name variable passed from the view.
Django
{{ user_name }}
Accesses the price attribute of a product object.
Django
{{ product.price }}
Shows the message variable in uppercase using a filter.
Django
{{ message|upper }}
Sample Program

This example shows how to pass variables user_name and message from the Django view to the template. The template uses double braces to display these values inside the HTML.

Django
{% comment %} views.py {% endcomment %}
from django.shortcuts import render

def home(request):
    context = {'user_name': 'Alice', 'message': 'Welcome to Django!'}
    return render(request, 'home.html', context)


{# home.html #}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home</title>
</head>
<body>
    <h1>Hello, {{ user_name }}!</h1>
    <p>{{ message }}</p>
</body>
</html>
OutputSuccess
Important Notes

Always pass the variables you want to use in templates from your Django views.

Double braces only display variables; to run logic, use Django template tags like {% if %} or {% for %}.

Summary

Double braces {{ }} show variables inside Django templates.

Variables come from the view and can be simple values or object attributes.

This keeps your HTML clean and separates design from Python code.