Generic Views
Generic Views
students and detailview that displays student details for any selected student in the list
In the proj->urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('general.urls')),
]
Template->student_detail.html
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
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
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
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: