For students enrolment create a generic class view which displays list of students and detail view that displays student details for any selected student in the list.

8 a] For students enrolment create a generic class view which displays list of students and detail view that displays student details for any selected student in the list.

To create a generic class-based view in Django that handles student enrollment, we’ll create two views:

  1. StudentListView: Displays a list of all students.
  2. StudentDetailView: Displays the details of a selected student.

Step 1: Define the Model

First, ensure that you have a Student model defined in your models.py file. Here’s a basic example:

# models.py

from django.db import models

class Student(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    enrollment_number = models.CharField(max_length=20, unique=True)
    date_of_birth = models.DateField()
    email = models.EmailField(unique=True)
    enrolled_date = models.DateField(auto_now_add=True)

    def __str__(self):
        return f"{self.first_name} {self.last_name}"

    def get_absolute_url(self):
        from django.urls import reverse
        return reverse('student_detail', kwargs={'pk': self.pk})

Step 2: Create the Views

Next, create the generic views in your views.py file:

# views.py

from django.views.generic import ListView, DetailView
from .models import Student

class StudentListView(ListView):
    model = Student
    template_name = "student_list.html"
    context_object_name = "students"

class StudentDetailView(DetailView):
    model = Student
    template_name = "student_detail.html"
    context_object_name = "student"

Step 3: Define the URLs

Add the URLs for the list and detail views in your urls.py file:

# urls.py

from django.urls import path
from .views import StudentListView, StudentDetailView

urlpatterns = [
    path('students/', StudentListView.as_view(), name='student_list'),
    path('students/<int:pk>/', StudentDetailView.as_view(), name='student_detail'),
]

Step 4: Create the Templates

Create the templates to display the list of students and the details of a selected student.

a. Template for Student List (student_list.html)
<!-- student_list.html -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student List</title>
</head>
<body>
    <h1>Student List</h1>
    <ul>
        {% for student in students %}
            <li>
                <a href="{% url 'student_detail' student.pk %}">
                    {{ student.first_name }} {{ student.last_name }} ({{ student.enrollment_number }})
                </a>
            </li>
        {% endfor %}
    </ul>
</body>
</html>
b. Template for Student Detail (student_detail.html)
<!-- student_detail.html -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student Detail</title>
</head>
<body>
    <h1>{{ student.first_name }} {{ student.last_name }}</h1>
    <p><strong>Enrollment Number:</strong> {{ student.enrollment_number }}</p>
    <p><strong>Date of Birth:</strong> {{ student.date_of_birth }}</p>
    <p><strong>Email:</strong> {{ student.email }}</p>
    <p><strong>Enrolled Date:</strong> {{ student.enrolled_date }}</p>
    <a href="{% url 'student_list' %}">Back to Student List</a>
</body>
</html>
Step 5: Run the Server

Finally, make sure everything is set up correctly:

  1. Run migrations if you haven’t done so already:
   python manage.py makemigrations
   python manage.py migrate
  1. Start the Django development server:
   python manage.py runserver

Leave a Reply

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