Display Data

Displaying data from your Django models to the user!

It’s 4:28 PM in Hyderabad on this lovely January 31, 2026 afternoon — perfect time to see your hard work (models, migrations, inserts) finally appear beautifully on the screen.

In this session we’ll focus on all the common and practical ways to display data in Django — from the simplest possible view to full-featured, production-ready patterns used in 2026.

We’ll use our existing polls app (with Question and Choice models).

Goal: Show data in these common scenarios

  1. List of all/latest questions (index/home page)
  2. Detail page for one question + its choices
  3. Results page with vote counts
  4. Bonus: Filtered list, pagination, admin-style display

Step 1: Quick Reminder — What We Already Have

Models (simplified):

Python

You should have some data already (via admin or shell).

Pattern 1: Function-Based View + Simple Template (Most Beginner-Friendly)

views.py (polls/views.py)

Python

urls.py (polls/urls.py)

Python

Template: polls/templates/polls/index.html

HTML

→ Result: Clean list of latest questions with links to detail pages using slug (SEO-friendly!)

Pattern 2: Detail View – Show One Question + Choices

views.py

Python

urls.py (better than pk!)

Python

Template: polls/templates/polls/detail.html

HTML

Pattern 3: Class-Based View (Generic) – Less Code, More Power

Many 2026 projects prefer this for lists & details.

views.py

Python

urls.py

Python

→ Much less code, automatic 404 handling, easy pagination (add {% if is_paginated %} in template)

Pattern 4: Display Aggregated Data (Vote Results)

views.py

Python

Template snippet:

HTML

Quick Comparison Table – Which Pattern to Choose?

Situation Recommended Pattern Why?
Simple list, few lines Function-based view Easy to understand, full control
Standard list/detail Generic ListView / DetailView Less code, built-in pagination, 404 handling
Complex filtering/sorting Function-based + get_queryset() Maximum flexibility
API-like (JSON) Django REST framework (later topic)
Admin-style table django-tables2 or admin custom

Your Homework / Next Steps

  1. Implement both function-based and class-based versions of index + detail
  2. Add some questions with different categories & slugs in admin
  3. Visit /polls/ and /polls/your-slug-here/
  4. Try pagination on the list view (add {% load bootstrap_pagination %} or simple links)

Tell me:

  • “It worked! Now show me how to add pagination nicely”
  • “How to display related choices with vote percentage bar?”
  • “I want to show only questions from last 7 days”
  • “Got TemplateDoesNotExist or NoReverseMatch — help!”
  • Or next big topic: Forms + Voting (POST handling)

You’re now displaying real data like a pro — the app is coming alive! 🌟🚀

Keep going, boss — you’re doing fantastic! 🇮🇳

You may also like...

Leave a Reply

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