Chapter 1: Django Prepare Template

Django Prepare Template: How to properly prepare and organize templates — so that your project stays clean, scalable, maintainable, and doesn’t turn into a nightmare when you add the 5th app or try to deploy.

Many people (especially in the first 2–3 months) just throw everything into one big templates/ folder or even worse — write HTML directly in views with HttpResponse. We are going to fix that mindset today.

I’ll explain like I’m sitting next to you in the café, showing you my screen, and we’ll build everything step-by-step in your existing polls project.

Goal for Today

We want to end up with:

  • A clean, reusable base template (navbar, footer, CSS links…)
  • Namespaced app templates (no name clashes when you add more apps)
  • Template inheritance ({% extends %} + {% block %})
  • Static files integration basics
  • Context processors & global variables
  • Common folder structure that almost every serious Django project in 2026 uses

Step 1: Understand Django’s Template Lookup Rules (Very Important)

Django looks for templates in these places (in this order):

  1. Folders listed in settings.py → TEMPLATES[‘DIRS’] (project-level templates)
  2. Each app’s templates/ subfolder — ifAPP_DIRS: True (default in new projects)
  3. Inside installed packages (third-party apps)

Golden rule for 2026:

  • Never put templates directly in project root templates/ unless they are truly global (like 404.html, 500.html, base.html)
  • Always put app-specific templates inside app/templates/appname/

Example correct structure:

text

Step 2: Create the Namespaced Template Folder (If Not Already Done)

Inside polls/:

Bash

Now every template file goes into polls/templates/polls/

Why the extra polls/ folder?

→ Prevents name collision. Imagine you later add a blog app that also has index.html. Django would get confused without the namespace.

When you write:

Python

Django knows: “look inside polls app → templates → polls → index.html”

Step 3: Create a Beautiful base.html (The Foundation)

Put this in polls/templates/polls/base.html (or in project templates/base.html if you prefer global)

HTML

Step 4: Use It in Child Templates (Inheritance)

Now update polls/templates/polls/index.html:

HTML

Do the same for detail.html, results.html — just change {% block content %}.

Step 5: Pass Global Variables via Context Processor (Optional but Nice)

In mysite/settings.py → TEMPLATES → OPTIONS → ‘context_processors’:

Make sure these are there (usually default):

Python

Add a custom one for current year:

Create mysite/context_processors.py:

Python

Then in settings.py → add to context_processors:

Python

Now {{ current_year }} and {{ site_name }} work in every template without passing them manually.

Step 6: Quick Checklist – Did You Prepare Templates Correctly?

  • All app templates are in app/templates/appname/
  • You use {% extends “polls/base.html” %} in every page
  • You use {% block title %}, {% block content %}, maybe {% block extra_css %}
  • You use {% url ‘polls:detail’ question.slug %} — never hardcode URLs
  • You use filters: |date, |timesince, |pluralize, |default, |title
  • You have basic CSS in base.html (or link to static later)

Common Beginner Mistakes (and Fixes)

  • TemplateDoesNotExist: index.html → Forgot the polls/ namespace → should be ‘polls/index.html’
  • Block not showing → Forgot {% block content %} in child or mismatched name
  • CSS not loading → Later we’ll move to {% static %} — don’t put <link> yet
  • NoReverseMatch → Forgot app_name = ‘polls’ in polls/urls.py

Your Mini Homework

  1. Create base.html as shown
  2. Update index.html and detail.html to extend it
  3. Add 2–3 questions in admin → visit /polls/
  4. Try adding {% block extra_head %} in child template for page-specific CSS

Tell me next:

  • “Done! Now show me how to add static files (CSS, JS, images) properly”
  • “How to make navbar highlight current page?”
  • “I want to use Tailwind or Bootstrap – how?”
  • “Got error: TemplateSyntaxError – help!”
  • Or next topic: Forms & Voting (POST requests)

You’re doing amazing — templates are now clean and professional! Let’s keep building this thing into something you can show your friends 😄🚀

You may also like...

Leave a Reply

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