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.
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")),
]
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})
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})
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>
| Example route | What 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.
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})
?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.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"))
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.
app_name and namespaced routes.students while the detail template expects student.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
Yes. Use {% url "students:detail" student.pk %} to construct the URL rather than manually concatenating strings.
Use filter() for a list of matches, and get_object_or_404() for a single required record in an HTTP view.
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.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.