← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 2 of 10

Rendering Templates from Views

A view's job is to pick a worksheet (template) and hand it a folder of answers (context) so Django can print the finished page.

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.

markup
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.

markup
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.

markup
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. #}
Common Mistakes
  1. Forgetting to pass a context dictionary, so template variables render as empty instead of the intended data.
  2. Hardcoding the template path incorrectly (missing the app subfolder) instead of matching the actual templates/ directory layout.
  3. Returning an HttpResponse with raw HTML strings instead of using render(), losing the benefits of the template engine.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.