Chapter 10: Django Templates

Django: Templates!

It’s still that beautiful Hyderabad afternoon (January 31, 2026, around 4:00 PM IST), and you’ve already got:

  • Project (mysite)
  • App (polls)
  • Views (function-based or class-based)
  • URLs wired up

Now templates turn your raw data into beautiful (or at least readable) HTML pages. Templates are not Python code — they’re HTML with special tags and filters that let Django insert dynamic content.

Django’s template engine (called Django Template Language or DTL) is intentionally not full Python — it’s safe for designers/non-coders, prevents arbitrary code execution, and encourages clean separation.

In Django 6.0.1 (today’s version), templates got a very nice new feature: Template Partials (small reusable named fragments inside the same file). We’ll use that too!

Step 1: Understand Template Setup (Where Files Go)

Django looks for templates in specific places. Recommended structure (namespaced to avoid clashes):

text

Why polls/ inside templates/?

  • Prevents name collision if another app has index.html
  • When you do render(request, ‘polls/index.html’) → Django knows exactly where

Add to mysite/settings.py (usually already there in new projects):

Python

APP_DIRS: True = Django auto-searches each installed app’s templates/ folder.

Step 2: First Simple Template (No Inheritance Yet)

In your polls/views.py (update index view):

Python

Create polls/templates/polls/index.html:

HTML

Key elements:

  • {{ variable }} → outputs escaped value (safe from XSS)
  • {% tag %} → logic/control (if, for, block…)
  • Filters: |date:”d M Y” → formats date nicely (31 Jan 2026)
  • {% if %} / {% for %} — no parentheses needed, very readable

Run server → http://127.0.0.1:8000/polls/ → dynamic list!

Step 3: Template Inheritance + base.html (Don’t Repeat Yourself!)

Almost every real site has a shared layout (navbar, footer, CSS links).

Create polls/templates/polls/base.html:

HTML

Now update index.html to extend it:

HTML
  • {% extends “polls/base.html” %} — inherits everything
  • {% block title %} / {% block content %} — override sections
  • {% url ‘polls:detail’ question.id %} — safe named URL (no hardcoding!)

Step 4: New in Django 6.0 – Template Partials! (Very Cool for 2026)

Partials = small named reusable chunks inside the same template file — great for HTMX, repeated cards, modals, without extra files.

Example in detail.html:

HTML
  • {% partialdef name %} … {% endpartialdef %} — define
  • {% partial “name” %} — render it (can pass context too in future)
  • Perfect for repeating UI bits without custom tags

Step 5: Useful Built-in Tags & Filters (Daily Essentials)

  • {% for %} / {% empty %} / {% endfor %}
  • {% if %}, {% elif %}, {% else %}
  • {% url ‘name’ arg1 arg2 %}
  • Filters: {{ var|upper }}, {{ var|default:”N/A” }}, {{ var|length }}, {{ var|date:”SHORT_DATE_FORMAT” }}, {{ var|pluralize }}
  • {% load static %} + {% static ‘css/style.css’ %} (for static files later)

Step 6: Quick Tips & Common Mistakes

  • Always use render(request, ‘app/template.html’, context) — not manual loader
  • Context must be a dict
  • Templates auto-escape HTML → safe, but use |safe if trusted (careful!)
  • Debug: {{ var }} → shows value; add {{ debug }} for full context
  • No Python code in templates → that’s the point!

Homework / Next Steps

  1. Create detail.html and results.html using inheritance
  2. Add a link from index to detail using {% url %}
  3. Try the partial for choice display
  4. Add some questions/choices via admin → see dynamic data

Tell me:

  • Did the page render? Any TemplateDoesNotExist error?
  • Want to add static files (CSS/JS/images) next?
  • Or forms / voting full flow?
  • Or “explain {% load %} and custom tags”?

You’re building a real app now — templates make it shine! 🌟 Keep going, boss! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *