Rendering Templates from Views
The render() Shortcut
render() is a Django shortcut function that loads a template, fills it in with a context dictionary, and wraps the result in an HttpResponse -- all in one call.
Note: render() is almost always preferred over manually building HttpResponse objects with HTML strings.
Example: The render() Shortcut
render() takes the request, the template path, and a context dict, returning a ready-to-send HttpResponse.
from django.shortcuts import render
def home(request):
context = {'site_name': 'CookiesCursor'}
return render(request, 'core/home.html', context)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Passing Data Through Context
The context dictionary's keys are exactly the variable names your template can reference with {{ }}.
Warning: If a key in context doesn't match any {{ }} in the template, Django simply ignores it -- no error is raised.
Example: Passing Data Through Context
The template can now use {{ username }} and {% if is_admin %} because those exact keys were passed in.
def profile(request):
context = {
'username': 'daniel',
'is_admin': False,
}
return render(request, 'core/profile.html', context)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Rendering Without Extra Context
If a template needs no dynamic data, you can call render() with just the request and template name -- the context argument is optional.
Example: Rendering Without Extra Context
Omitting context is valid when the template is fully static.
def about(request):
return render(request, 'core/about.html')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Forgetting to pass a context dictionary, so template variables render as empty instead of the intended data.
- Hardcoding the template path incorrectly (missing the app subfolder) instead of matching the actual templates/ directory layout.
- Returning an HttpResponse with raw HTML strings instead of using render(), losing the benefits of the template engine.
- render(request, template_name, context) is the standard shortcut for rendering a template with data and returning an HttpResponse.
- The context dictionary's keys become the variable names available inside the template.
- render() automatically handles request-related context processors for you.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: