I'm making a student management program.
Currently, the paging and search function have been coded as query and it works in combination, but since we receive new students every year, school_year
is important.
I want to see the student list by school_year
that I want by pressing the year on the top right.
I've been thinking about implementing it as a query, but it seems too complicated (I think it's too complicated to combine with the queries that have already been created), and it's too unnecessary to make a listview for each grade.
pls give me some good idea or solution
views.py:
class StudentList(ListView):
model = Student
template_name = 'student/orders.html'
context_object_name = 'students'
paginate_by = 12
def get_queryset(self):
keyword = self.request.GET.get('keyword')
if keyword:
object_list = self.model.objects.filter(
Q(name__icontains=keyword) | Q(email__icontains=keyword) | Q(student_mobile__icontains=keyword)
)
else:
object_list = self.model.objects.all()
return object_list
def get_context_data(self, **kwargs):
context = super(StudentList, self).get_context_data(**kwargs)
paginator = context['paginator']
page_numbers_range = 5 # Display only 5 page numbers
max_index = len(paginator.page_range)
page = self.request.GET.get('page')
current_page = int(page) if page else 1
start_index = int((current_page - 1) / page_numbers_range) * page_numbers_range
end_index = start_index + page_numbers_range
if end_index >= max_index:
end_index = max_index
page_range = paginator.page_range[start_index:end_index]
context['page_range'] = page_range
# for combine filterign and paging
# https://stackoverflow.com/questions/51389848/how-can-i-use-pagination-with-django-filter
if self.request.GET.get('keyword'):
keyword = self.request.GET.copy()
if self.request.GET.get('page'):
del keyword['page']
context['keyword'] = keyword.urlencode()
return context
class StudentDetail(DetailView):
model = Student
template_name = 'student/member.html'
context_object_name = 'student'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['pk'] = Student.objects.filter(pk=self.kwargs.get('pk'))
return context