0% found this document useful (0 votes)
3 views

Generic Views

Uploaded by

Srihari Murali
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Generic Views

Uploaded by

Srihari Murali
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

For students enrolment developed in Module 2, create a generic class view which displays list of

students and detailview that displays student details for any selected student in the list

In the proj->urls.py

from django.contrib import admin


from django.urls import path,include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('general.urls')),
]
Template->student_detail.html

<h1>{{ student.name }}</h1>


<p>Age: {{ student.age }}</p>
<p>Grade: {{ student.grade }}</p>

Template->student_list.html

<h1>Student List</h1>
<ul>
{% for student in students %}
<li><a href="{% url 'student-detail' student.pk %}">{{
student.name }}</a></li>
{% endfor %}
</ul>

In the app->views.py

from django.views.generic import ListView, DetailView


from .models import Student

class StudentListView(ListView):
model = Student
template_name = 'student_list.html' # Your template for listing students
context_object_name = 'students' # Optional: Default context variable
name is 'object_list'

class StudentDetailView(DetailView):
model = Student
template_name = 'student_detail.html' # Your template for displaying
student details
context_object_name = 'student' # Optional: Default context variable name
is 'object'
In the app->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'),
]
In the app->models.py

class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
grade = models.CharField(max_length=10)

def __str__(self):
return self.name
in the app->admin.py

from django.contrib import admin


from general.models import Student
# Register your models here.
admin.site.register(Student)
Develop Django app that performs CSV and PDF generation for any models created

Install Required Libraries:

First, make sure you have django-import-export for CSV generation and reportlab for
PDF generation installed in your Django project. If not, you can install them using pip:

You might also like