Django

Create a Detail Page to Display Information of a Single Record

πŸ“Œ 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

  • id is received from URL
  • get() 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 student object 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

  1. User clicks β€œView Details”
  2. URL passes student ID
  3. View fetches that record
  4. 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

  1. Add β€œBack to list” link
  2. Show student photo (if added)
  3. Display only selected fields
  4. Add edit/delete buttons
  5. 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

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

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