Django templates separate presentation from Python logic. The template language is deliberately limited—no arbitrary code execution, reducing XSS risk when combined with auto-escaping.
Template syntax
{{ article.title }}
{% for article in articles %}
<h2>{{ article.title }}</h2>
{% endfor %}
{% if user.is_authenticated %}Welcome{% endif %}
Template loading
Place templates in app/templates/app/ to avoid name clashes. Configure TEMPLATES['DIRS'] for project-wide templates. Use {% extends "base.html" %} and {% block content %} for layouts.
Important interview questions and answers
- Q: Logic in templates?
A: Minimal—complex logic belongs in views or custom template tags; templates display data. - Q: Auto-escaping?
A: On by default—{{ user_input }}escapes HTML unless marked safe (careful with|safe). - Q: Static vs template?
A: Templates are rendered per request with context; static files (CSS/JS) are served as-is fromstatic/.
Self-check
- What delimiter prints a variable?
- Why avoid heavy logic in templates?