What are Django templates and how does the template language work?
Learn what Django templates are and how the Django Template Language works: variables, tags, filters, inheritance and auto-escaping, with examples.
Expected Interview Answer
Django templates are text files (usually HTML) that mix static markup with the Django Template Language, letting a view pass data into the page for rendering; the template engine replaces variables and runs tags to produce the final response.
The Django Template Language (DTL) uses {{ variable }} to output values, {% tag %} for logic like loops and conditionals ({% for %}, {% if %}), and filters like {{ name|upper }} or {{ date|date:'Y-m-d' }} to transform output. Templates support inheritance through {% extends %} and {% block %}, so a base layout defines blocks that child pages override. DTL is intentionally restricted — it discourages heavy logic in templates, and Django auto-escapes variables to help prevent XSS.
- Separates presentation from business logic
- Template inheritance removes duplicated layout markup
- Filters transform data without extra view code
- Auto-escaping guards against XSS by default
- Readable syntax that non-Python designers can edit
AI Mentor Explanation
A Django template is like a printed scorecard form with blank fields for names and runs: the layout is fixed, but the numbers are filled in each match. The template tags are the rules the scorer follows — loop through each batter, show a total only if the innings ended. The view supplies the match data, and the engine fills the form to produce the finished scorecard.
Step-by-Step Explanation
Step 1
Create a template
Add an HTML file under a templates/ directory that Django's loaders can discover.
Step 2
Render from a view
Call render(request, 'page.html', context) to pass a context dict of data into the template.
Step 3
Output variables
Use {{ variable }} to display context values and {{ value|filter }} to transform them.
Step 4
Add logic with tags
Use {% for %}, {% if %} and other tags for loops and conditionals within the markup.
Step 5
Use inheritance
Define blocks in a base with {% block %} and override them in children via {% extends %}.
What Interviewer Expects
- Knows templates separate presentation from logic
- Understands {{ variables }}, {% tags %} and filters
- Can explain template inheritance with extends and block
- Aware of auto-escaping and XSS protection
- Knows the view passes a context dict to the template
Common Mistakes
- Putting heavy business logic in templates instead of views
- Confusing {{ }} for output with {% %} for logic tags
- Forgetting {% extends %} must be the first line of a child template
- Disabling auto-escaping without understanding XSS risk
- Not passing needed data into the context dict
Best Answer (HR Friendly)
“Django templates are HTML files with special slots where live data gets inserted when a page loads. They use a simple language for showing values, looping over lists, and reusing a shared layout, so designers can build pages without writing much code.”
Code Example
<!-- base.html -->
<html>
<body>
<header>My Site</header>
{% block content %}{% endblock %}
</body>
</html>
<!-- post_list.html -->
{% extends 'base.html' %}
{% block content %}
<h1>{{ title|upper }}</h1>
<ul>
{% for post in posts %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
{% endblock %}Follow-up Questions
- What is the difference between {{ }} and {% %} in DTL?
- How does template inheritance with extends and block work?
- What are template filters and can you name a few?
- How does Django protect against XSS in templates?
- How do custom template tags and filters get registered?
MCQ Practice
1. Which syntax outputs a variable in a Django template?
Double curly braces {{ }} evaluate and output a context variable, while {% %} is for logic tags.
2. Which tag lets a child template reuse a base layout?
{% extends %} makes a template inherit a base and override its {% block %} sections.
3. What does Django do to variables by default to prevent XSS?
Django auto-escapes variable output so injected HTML is rendered as text, mitigating XSS.
Flash Cards
What is a Django template? — A text/HTML file mixing static markup with the Django Template Language, filled with view data at render time.
{{ }} vs {% %}? — {{ }} outputs a variable; {% %} runs logic tags like for, if, extends and block.
What is a template filter? — A transform applied with a pipe, e.g. {{ name|upper }} or {{ date|date:'Y-m-d' }}.
How does template inheritance work? — A base defines {% block %} regions that children override using {% extends %}.