Templates
A Django template is an HTML file containing the Django Template Language — placeholders and tags that Django fills in with data from your views to produce the final page sent to the browser.
Learn Templates in our free Django course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Django course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll learn how to render a template from a view, pass data through a context dictionary, output variables, drive logic with template tags, and reshape values with filters.
A view's job is to gather data and hand it to a template. The render() shortcut takes the request, the template's filename, and a context dictionary, then returns a finished HttpResponse with the rendered HTML.
The keys of the context dictionary become the variable names you can use inside the template. So if your context is {" "} , the template can output that value with {'{ }'} .
And the matching template file might look like this:
The Django Template Language has two main building blocks. Variables use the double-brace syntax to print a value: {'{ }'} . You can even reach into attributes and dictionary keys with a dot.
Tags use the brace-percent syntax to run logic. The {' '} tag loops over a list and the {' '} tag branches. Block tags must be closed: a for-loop ends with {' '} and an if ends with {' '} .
A filter transforms a variable just before it is displayed. You apply one with a pipe character after the variable name. Some filters take an argument after a colon, and you can chain several together.
Fill in the blank so the context dictionary has a username key that the template variable {'{ }'} would print as jordan .
Django cannot find the file because the templates directory is not configured or the name is misspelled.
✅ Fix: set APP_DIRS to True or add your folder to DIRS in the TEMPLATES setting, and confirm the filename matches.
❌ Invalid block tag: 'endblock', expected 'endfor'
You opened a {' '} or {' '} but never closed it.
✅ Fix: every block tag needs its closing tag — add {' '} or {' '} .
❌ Could not parse the remainder of a Python expression
You wrote raw Python (like a function call) where a template tag is needed.
✅ Fix: use a tag such as {' '} instead of a Python expression — the template language is not Python.
Build the HTML a template would produce for a list of products, applying an uppercase transform to each name like the upper filter would.
Lesson 4 complete — your views can now render real pages!
You now know how render() combines a template with a context dictionary, how to output variables with double braces, how to drive logic with the for and if tags, and how to reshape values with filters like upper, date, length, default, and truncatewords.
🚀 Up next: Static Files — learn how to serve CSS, JavaScript, and images alongside your templates.
Practice quiz
What does the render() shortcut take and return?
- A model and returns a QuerySet
- The request, a template name, and a context, returning an HttpResponse
- A URL pattern and returns a view
- A form and returns JSON
Answer: The request, a template name, and a context, returning an HttpResponse. render(request, template, context) fills the template with context and returns an HttpResponse.
Where do the context dictionary's keys become usable in a template?
- Only in the URL pattern
- Only inside {% block %} tags
- As variable names inside double braces, e.g. {{ title }}
- As filenames Django loads
Answer: As variable names inside double braces, e.g. {{ title }}. Each context key becomes a variable name you output with the double-brace syntax.
Which syntax outputs a value into the page?
- {{ value }}
- {% value %}
- {# value #}
- << value >>
Answer: {{ value }}. Double braces {{ value }} output a variable's value into the rendered HTML.
Which syntax is used for logic such as loops and branching?
- {{ ... }}
- {# ... #}
Brace-percent {% ... %} tags run logic like {% for %} and {% if %}.
How must a {% for %} loop be closed?
- With {% endloop %}
- With {% endfor %}
- With {% end for %}
- It closes automatically
Answer: With {% endfor %}. Block tags must be closed; a for loop ends with {% endfor %}.
How do you apply a filter to a variable?
- With a pipe after the variable, e.g. {{ name|upper }}
- With a colon before it, e.g. {{ :upper name }}
- By wrapping it in {% filter %}
- Filters cannot be applied in templates
Answer: With a pipe after the variable, e.g. {{ name|upper }}. A filter is applied with the pipe character after the variable name, like {{ name|upper }}.
What does the upper filter do in {{ name|upper }}?
- Counts the characters in name
- Supplies a default value
- Converts the text to uppercase
- Formats name as a date
Answer: Converts the text to uppercase. The upper filter converts the variable's text to uppercase.
How do you pass an argument to a filter like date?
- With a pipe, as {{ joined|date|'F j, Y' }}
- With a space, as {{ joined date 'F j, Y' }}
- Filters never take arguments
- With a colon, as {{ joined|date:'F j, Y' }}
Answer: With a colon, as {{ joined|date:'F j, Y' }}. An argument follows the filter after a colon, e.g. {{ joined|date:'F j, Y' }}.
What does TemplateDoesNotExist usually indicate?
- Django could not find the named template file or its directory is not configured
- The template has a syntax error
- A context key was missing
- The view returned None
Answer: Django could not find the named template file or its directory is not configured. It means Django could not locate the template — check DIRS/APP_DIRS and the filename.
Which is the correct call to render a template with context in a view?
- render('x.html', context)
- render_template(request, 'x.html')
- render(request, 'x.html', context)
- template.render('x.html', request)
Answer: render(request, 'x.html', context). render(request, 'x.html', context) is the standard shortcut for rendering a template.