π Introduction
In many applications, we first display a list of records, and when the user clicks on one item, we show its detailed information.
Examples:
- Student details page
- Product details page
- Profile page
In this program, we will:
- fetch a single student record
- pass it to template
- display full details
π― Program Statement
π Create a detail page to display information of a single record.
π§ Concept
This program introduces:
- URL parameter (
id) get()method- dynamic routing
- linking pages
βοΈ Step 1: Create View
π File: views.py
πΉ Path:
myproject/myapp/views.py
πΉ Code:
from django.shortcuts import render
from .models import Student
def student_detail(request, id):
student = Student.objects.get(id=id)
return render(request, 'student_detail.html', {
'student': student
})
π§ Explanation
idis received from URLget()fetches a single record- That record is passed to template
βοΈ Step 2: URL Mapping with Parameter
π File: urls.py
πΉ Path:
myproject/myproject/urls.py
πΉ Code:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('student/<int:id>/', views.student_detail, name='student_detail'),
]
π§ Explanation
<int:id>captures ID from URL- Example URL:
/student/1/
βοΈ Step 3: Create Template
π File: student_detail.html
πΉ Path:
myproject/templates/student_detail.html
πΉ Code:
<!DOCTYPE html>
<html>
<head>
<title>Student Detail</title>
</head>
<body>
<h1>Student Detail Page</h1>
<p><strong>ID:</strong> {{ student.id }}</p>
<p><strong>Name:</strong> {{ student.name }}</p>
<p><strong>Age:</strong> {{ student.age }}</p>
<p><strong>Course:</strong> {{ student.course }}</p>
<p><strong>Email:</strong> {{ student.email }}</p>
</body>
</html>
π§ Explanation
- Displays full details of one student
- Uses
studentobject passed from view
βοΈ Step 4: (Important) Link from Table/List Page
π Modify Post 46 table to add a link:
π File: student_table.html
πΉ Add inside loop:
<td>
<a href="/student/{{ s.id }}/">View Details</a>
</td>
πΉ Updated Row Example:
<tr>
<td>{{ s.id }}</td>
<td>{{ s.name }}</td>
<td>{{ s.age }}</td>
<td>{{ s.course }}</td>
<td>{{ s.email }}</td>
<td><a href="/student/{{ s.id }}/">View Details</a></td>
</tr>
π§ Explanation
- Clicking link sends ID to detail view
- Dynamic URL is created using
{{ s.id }}
βοΈ Step 5: Run Server
python manage.py runserver
π Step 6: Output
π Open:
http://127.0.0.1:8000/student/1/
β Example Output:
Student Detail Page ID: 1 Name: Kanchan Age: 25 Course: MCA Email: kanchan@example.com
π§ How It Works
- User clicks βView Detailsβ
- URL passes student ID
- View fetches that record
- Template displays details
π₯ Key Concepts
Fetch Single Record
Student.objects.get(id=id)
URL Parameter
<int:id>
Captures ID from URL.
Dynamic Link
<a href="/student/{{ s.id }}/">
Template Variable
{{ student.name }}
β οΈ Common Errors
β Record not found error
If ID does not exist:
DoesNotExist error
π Better approach:
from django.shortcuts import get_object_or_404
student = get_object_or_404(Student, id=id)
β URL mismatch
Ensure URL pattern matches:
student/<int:id>/
β Wrong variable name
Template must use:
student
β Hardcoded links
Better to use {% url %} (advanced):
{% url 'student_detail' s.id %}
π§ͺ Practice Questions
- Add βBack to listβ link
- Show student photo (if added)
- Display only selected fields
- Add edit/delete buttons
- Handle invalid ID using
get_object_or_404()
π€ Viva Questions & Answers
1. What is a detail view in Django?
A detail view displays information about a single record from the database.
2. What is get() method?
It fetches a single object based on a condition like ID.
3. What happens if record is not found?
Django raises DoesNotExist error.
4. What is <int:id> in URL?
It captures an integer value from the URL and passes it to the view.
5. Why do we pass ID in URL?
To identify which specific record should be fetched.
6. What is dynamic routing?
It means URL changes dynamically based on data like ID.
7. What is get_object_or_404()?
It fetches the object or shows a 404 error if not found.
8. How do we link list page to detail page?
Using anchor tag with dynamic ID.
9. Why is detail view important?
It allows users to see full information of a selected record.
10. Where is logic written β template or view?
Logic is written in view, template only displays data.
π Next Post: Create a ModelForm to Add Records to the Database
π Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
