Chapter 2: Django Add Link to Details

Django Add Link to Details
You now have:

  • Models with Question + Choice
  • Data inserted/updated
  • Templates prepared with base.html + inheritance
  • List view showing latest questions

The next very important real-world step is: Make each question in the list clickable → link to its detail page

This is one of those small things that separates a “toy project” from something that feels like a real application.

We’re going to do this very carefully, step by step, covering:

  • The correct way using {% url %} tag (never hardcode!)
  • Using slug vs pk in URLs
  • Handling the link in list view
  • Updating detail view & URL pattern
  • Common mistakes & debugging tips

Let’s build it together.

Step 1: Decide URL Style for Detail Page

Two common choices in 2026:

Style URL example Pros Cons When to use
Numeric ID (pk) /polls/42/ Simple, short Not SEO-friendly, ugly Internal tools, admin-like apps
Slug (readable) /polls/best-biryani-hyderabad/ Beautiful, SEO, shareable Needs unique slug field Public-facing sites (most cases)

Recommendation for your polls app: Use slug — it looks professional and teaches you a very common pattern.

(If you don’t have slug field yet → add it now, see previous “Update Model” lesson)

Step 2: Update URL Pattern for Detail (Use Slug)

In polls/urls.py:

Python
  • <slug:slug> → built-in converter: letters, numbers, hyphens, underscores
  • name=’detail’ → we’ll use this in templates

Step 3: Update Detail View to Use Slug

In polls/views.py:

Python

Step 4: Add the Link in the List Template (The Main Goal!)

Open polls/templates/polls/index.html

Find the loop where you display questions (inside {% for question in latest_questions %}):

Before (probably plain text):

HTML

After (with proper link):

HTML

Or more beautiful card style (recommended):

HTML

Key points in the link:

  • {% url ‘polls:detail’ slug=question.slug %}
    • polls: → namespace from app_name
    • detail → name from urls.py
    • slug=question.slug → keyword argument matching <slug:slug> in path

Never write:

HTML

or

HTML

Step 5: Test It Live

  1. Make sure you have at least 2–3 questions with unique slugs (in admin: edit question → slug should auto-fill if you overrode .save())

  2. Run server:

    Bash
  3. Visit http://127.0.0.1:8000/polls/

  4. Click any question title or “View & Vote” button

→ You should land on the detail page showing that specific question + choices

Step 6: Debugging Common Problems

Problem Error Message / Symptom Fix
NoReverseMatch Reverse for ‘detail’ not found Add app_name = ‘polls’ in polls/urls.py
slug not passed url() missing 1 required positional argument Use keyword slug=question.slug (not positional)
404 on click Question matching query does not exist Check slug is unique & populated; use get_object_or_404
Link shows /polls/None/ question.slug is blank Populate slugs (data migration or .save() override)
Link works but shows wrong question Multiple questions same slug Add unique=True + run migration + fix duplicates

Bonus: Make Slug Auto-Unique (Pro Touch)

In models.py → improve .save():

Python

→ Prevents duplicates automatically (e.g. “best-biryani-2”)

Your Quick Homework

  1. Add the <a href=”{% url ‘polls:detail’ slug=question.slug %}”> in your index template
  2. Create 3 questions with different slugs
  3. Click around — make sure detail page loads correct data
  4. Try changing one slug in admin → refresh list → link updates automatically

Tell me next:

  • “Links work! Now show me how to add voting (POST form + F() update)”
  • “How to make slug links look nicer (remove ID completely)?”
  • “I want breadcrumbs or back button to list”
  • “Got NoReverseMatch or 404 – here’s screenshot/error”
  • Or next topic: Django Forms / ModelForm

You’re now connecting list → detail like a real web app — super proud of your progress! Let’s keep going — this is getting exciting 🚀🇮🇳

You may also like...

Leave a Reply

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