Python > Web Development with Python > Django > Templates (Django Template Language)
Rendering Dynamic Content in Django Templates
This snippet demonstrates how to render dynamic content from a Django view into an HTML template using the Django Template Language (DTL). It covers passing variables from the view to the template and using them within the template to display information.
Setting Up the Django View
This code defines a Django view named my_view
. It creates a dictionary called context
containing data that we want to pass to the template. The render
function takes the request, the template name ('my_template.html'), and the context dictionary as arguments. It then renders the template with the provided context and returns the HTTP response.
from django.shortcuts import render
def my_view(request):
context = {
'name': 'Alice',
'age': 30,
'hobbies': ['reading', 'hiking', 'coding']
}
return render(request, 'my_template.html', context)
Creating the Django Template (my_template.html)
This HTML file represents the Django template. It uses the double curly braces {{ ... }}
to access variables passed from the view's context. For example, {{ name }}
will be replaced with the value of the 'name' key in the context (in this case, 'Alice'). The {% for ... %}
tag is a loop that iterates over the 'hobbies' list and displays each hobby in a list item.
<!DOCTYPE html>
<html>
<head>
<title>My Template</title>
</head>
<body>
<h1>Hello, {{ name }}!</h1>
<p>You are {{ age }} years old.</p>
<h2>Hobbies:</h2>
<ul>
{% for hobby in hobbies %}
<li>{{ hobby }}</li>
{% endfor %}
</ul>
</body>
</html>
Concepts Behind the Snippet
The core concept is the separation of concerns: the view handles the application logic and data retrieval, while the template handles the presentation. The context dictionary acts as a bridge, transferring data from the view to the template. The Django Template Language provides a syntax for accessing variables, performing loops, and using conditional statements within the template.
Real-Life Use Case Section
Imagine a blog application. The view retrieves blog posts from a database. The template then uses the 'for' loop to display each blog post's title, content, and author. User profiles, product listings, and dynamic navigation menus are all common use cases.
Best Practices
{{ date|date:'Y-m-d' }}
).safe
filter carefully.
Interview Tip
Be prepared to explain the role of templates in the MVC (Model-View-Controller) architecture. Understand how data flows from the model to the view and then to the template. Also, be ready to discuss common template tags and filters.
When to Use Them
Use Django templates whenever you need to dynamically generate HTML or other text-based formats based on data from your application. They're ideal for creating user interfaces, reports, emails, and other dynamic content.
Alternatives
Alternatives to Django templates include:
Pros
Cons
FAQ
-
What is the difference between
{{ variable }}
and{% tag %}
?
{{ variable }}
is used to output the value of a variable from the context.{% tag %}
is used to execute template logic, such as loops, conditional statements, or custom tags. -
How can I pass data from my view to the template?
You pass data to the template using a dictionary called the 'context'. You include the context dictionary as an argument to the
render
function in your view.