python / beginner
Snippet
Rendering a Template
Templates allow you to separate the HTML design from the Python logic. You can pass data (context) to these templates for dynamic display.
snippet.py
1
2
3
4
5
from django.shortcuts import renderdef profile_page(request):context = {'username': 'Alex'}return render(request, 'profile.html', context)
django
Breakdown
1
context = {'username': 'Alex'}
A dictionary mapping variable names used in the template to Python objects.
2
return render(request, 'profile.html', context)
Combines a given template with a context dictionary and returns an HttpResponse.