Chapter 4: Django Add Main Index Page

Grab your coffee, sit comfortably, and let’s build the main index page of your Django project together. It’s around 5:00 PM in Hyderabad on January 31, 2026 — perfect time to make your app feel like a real website instead of just a collection of scattered pages.

Right now you probably have:

  • polls/ app with models (Question + Choice)
  • Some data in the database
  • polls/templates/polls/index.html showing a list of questions
  • polls/ urls pointing to /polls/ → polls app index
  • A nice base.html master template

But the root URL (http://127.0.0.1:8000/) still shows either:

  • the default Django rocket page, or
  • 404, or
  • nothing meaningful

Today we fix that → we create a proper main/home/index page at / (the domain root), which is what most visitors see first.

We’ll do this in the recommended 2026 way — clean, scalable, and following what most medium-sized Django projects actually do.

Two Realistic Choices for the Main Index Page

Approach When people use it Pros Cons Folder name people usually choose
Put everything in polls/ app Very small projects, learning phase Fewer folders Becomes messy when you add more features
Create small pages / core / home app Almost every real project (most recommended) Clean separation, conventional One extra app pages or core
Just TemplateView in project urls Super-minimal landing page (marketing only) Fastest possible Hard to grow (no models/views logic)

Best choice for you right now: Create a tiny app called pages (or core) → this is what ~70–80% of Django developers do in real projects in 2026

Step 1: Create the pages App

In your terminal (inside virtualenv, project root):

Bash

Add it to INSTALLED_APPS in mysite/settings.py:

Python

Now folder structure:

text

Step 2: Create pages/urls.py

Python

Step 3: Create a Simple Home View (Function-Based First)

pages/views.py

Python

(You can also use TemplateView class — we’ll see both)

Step 4: Connect It to Project URLs (Root Route)

Open mysite/urls.py (project level):

Python

Order matters Django matches URLs top-to-bottom. If you put path(”, …) first → nothing else would ever match.

Step 5: Create the Home Template (pages/templates/pages/home.html)

First create the folder structure:

Bash

Now pages/templates/pages/home.html:

HTML

Step 6: Test It!

Bash

Open: http://127.0.0.1:8000/

You should see:

  • Your nice header from base.html
  • Big welcome title
  • Message
  • Button linking to /polls/
  • Footer

→ No more rocket page! This is now your real homepage.

Bonus: Class-Based View Version (Many Teams Prefer This)

Alternative pages/views.py:

Python

Then in pages/urls.py:

Python

Same result — just more reusable when you want to add login checks, featured polls query, etc.

Quick Summary – What We Did

  1. Created small pages app
  2. Added simple home view
  3. Created namespaced pages/home.html
  4. Connected root URL ” to pages.urls
  5. Extended base.html → consistent design
  6. Added nice call-to-action button to polls

Common Mistakes & Quick Fixes

  • 404 at root → Forgot path(”, include(‘pages.urls’)) or put it before admin/
  • TemplateDoesNotExist: pages/home.html → Wrong folder → must be pages/templates/pages/home.html
  • NoReverseMatch for ‘polls:index’ → Forgot app_name = ‘polls’ in polls/urls.py
  • Old rocket page still shows → Check URL order — root path must be last

Your Mini Homework

  1. Create the pages app & home page exactly as shown
  2. Visit / → make sure it looks good
  3. Click the “Browse Available Polls” button → should go to /polls/
  4. (Optional) Add one nice hero image or statistic in the template

Tell me what feels next:

  • “It works! Now show me how to add a real featured polls section on home”
  • “How to make different homepage for logged-in vs anonymous users?”
  • “I want a more marketing-style landing page (hero + testimonials)”
  • “Got error – here is message/screenshot”
  • Or finally ready for: “Let’s implement voting form + POST + vote counting”

You’ve just created the front door of your app — visitors will see this first. You’re doing really well — let’s make it even better! 🚀🇮🇳

You may also like...

Leave a Reply

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