Django Student List and Detail Pages with MySQL

Recommended preparation: set up a Django project and read the Django tutorial overview before trying the code.

Create a Django list where each student name links to a record-detail page. Use named URL paths and get_object_or_404() instead of building links around unchecked ?id= query strings.

students/templates/students/student_list.html — clickable list

<h1>Students</h1>
<ul>
{% for student in students %}
  <li><a href="{% url 'students:detail' student.pk %}">{{ student.name }}</a> ({{ student.mark }})</li>
{% empty %}
  <li>No students have been added yet.</li>
{% endfor %}
</ul>

Records previously addressed through query strings such as ?id=6, ?id=7 and ?id=8 can also use named URL paths. Django path converters make the identifier explicit in the route and help keep the list and detail views separate.

On this page — Show page sectionsHide page sections

Define URL patterns

students/urls.py

from django.urls import path
from . import views

app_name = "students"
urlpatterns = [
    path("", views.student_list, name="list"),
    path("<int:pk>/", views.student_detail, name="detail"),
]

plus2net_demo/urls.py

from django.urls import include, path
from django.contrib import admin

urlpatterns = [
    path("admin/", admin.site.urls),
    path("students/", include("students.urls")),
]

Create the list view

students/views.py — list all students by ID

from django.shortcuts import render
from .models import Student

def student_list(request):
    students = Student.objects.order_by("id")
    return render(request, "students/student_list.html", {"students": students})

Create the detail view

students/views.py — detail page with 404 handling

from django.shortcuts import get_object_or_404, render
from .models import Student

def student_detail(request, pk):
    student = get_object_or_404(Student, pk=pk)
    return render(request, "students/student_detail.html", {"student": student})

Create the detail template

students/templates/students/student_detail.html

<h1>{{ student.name }}</h1>
<p>Class: {{ student.cl }}</p>
<p>Mark: {{ student.mark }}</p>
<p>Gender: {{ student.gender }}</p>
<a href="{% url 'students:list' %}">Back to students</a>

How the two pages work together

Example routeWhat the learner sees
/students/A list with clickable student names
/students/6/Details for the student whose primary key is 6
/students/999999/A 404 page if no matching record exists

Query-string identifiers also work in Django, but named URL paths are easier to maintain. For a dedicated explanation of collecting query parameters, see Django query strings.

Alternative: use a query-string URL

Optional approach if existing URLs must stay as ?id=6

from django.shortcuts import get_object_or_404, render
from .models import Student

def student_page(request):
    raw_id = request.GET.get("id")
    if raw_id is None:
        students = Student.objects.order_by("id")
        return render(request, "students/student_list.html", {"students": students})
    try:
        pk = int(raw_id)
    except ValueError:
        from django.http import HttpResponseBadRequest
        return HttpResponseBadRequest("Invalid student ID")
    student = get_object_or_404(Student, pk=pk)
    return render(request, "students/student_detail.html", {"student": student})
A link like ?id=6 is relative to the page where it appears. To test a Django example locally, use the configured /students/6/ route, not the tutorial page URL.

Add pagination for a large student list

Paginator yields a manageable collection of rows

from django.core.paginator import Paginator

students = Student.objects.order_by("id")
page = Paginator(students, 25).get_page(request.GET.get("page"))

Permissions and privacy

A successful ID lookup does not imply a visitor is authorized to see the record. For private student information, require authentication and enforce object-level authorization before rendering the detail. Do not expose real student records in a public teaching demo.

Troubleshooting

  • NoReverseMatch: confirm app_name and namespaced routes.
  • 404 for /students/6/: check the top-level URL include and verify a row with that ID exists.
  • Query-string ID fails: convert strings to integers and handle missing/invalid input.
  • Template errors: the list template expects students while the detail template expects student.

Improve the detail-page experience

More semantic detail-page markup

<nav aria-label="Student pages">
  <a href="{% url 'students:list' %}">All students</a>
</nav>
<h1>{{ student.name }}</h1>
<dl>
  <dt>Class</dt><dd>{{ student.cl }}</dd>
  <dt>Mark</dt><dd>{{ student.mark }}</dd>
</dl>

Login-protected detail view; add authorization as needed

from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, render
from .models import Student

@login_required
def student_detail(request, pk):
    student = get_object_or_404(Student, pk=pk)
    return render(request, "students/student_detail.html", {"student": student})

Avoid hard-coded numerical links

<a href="{% url 'students:detail' student.pk %}">
  View {{ student.name }}
</a>

Alternative query ID validation in a view

from django.http import Http404

try:
    pk = int(request.GET.get("id", ""))
except ValueError as exc:
    raise Http404("Invalid ID") from exc

Frequently asked questions

Can I link directly to an ID with a Django template tag?

Yes. Use {% url "students:detail" student.pk %} to construct the URL rather than manually concatenating strings.

Should I use filter() or get_object_or_404()?

Use filter() for a list of matches, and get_object_or_404() for a single required record in an HTTP view.

What if the list is empty?

The {% empty %} branch in the template displays a helpful message.

Previous: Display and filter MySQL rows

Next: Admin interface

Browse the complete Django tutorial series.

More references from this tutorial

  • ?id=6 — historical query example on this tutorial; Django uses its own local routes
  • ?id=7 — historical query example on this tutorial; Django uses its own local routes
  • ?id=8 — historical query example on this tutorial; Django uses its own local routes
  • ?id=9 — historical query example on this tutorial; Django uses its own local routes
  • ?id=10 — historical query example on this tutorial; Django uses its own local routes

Additional references

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer