0

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

listveiw template IMG

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
yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42
chea
  • 101
  • 1
  • 11

3 Answers3

0
  1. In your search page you can add selector with all possible years. Then, in your get_queryset_data add this:
year = self.request.GET.get('name_of_the_year_selector')
if year:
    object_list = object_list.filter(year=int(year))
return object_list

  1. If you want specific url for each year then you should add another path into urls.py:
path('student/<int:year>/, StudentsList.as_view(), 'students-by-year')

Then you can try to add this code into your StudentList:

def setup(self, **kwargs):
     self.year = kwargs['year']
def get_queryset(self)
    if self.year:
        return self.model.objects.filter(year=int(self.year)
    if keyword:
....
Andrey Borzenko
  • 718
  • 5
  • 8
0

i made it. thanks Borzenko

view.py

def get_queryset(self): #context over writing
    keyword = self.request.GET.get('keyword')
    school_year = self.kwargs.get('school_year')
    school_year = str(school_year)

    if keyword:
        object_list = self.model.objects.filter(
        Q(name__icontains=keyword) | Q(email__icontains=keyword) | 
        Q(student_mobile__icontains=keyword)
        ).filter(school_year=school_year)
    else:
        object_list = self.model.objects.all().filter(school_year=school_year)

    return object_list

urls.py

path('student/<str:school_year>/', views.StudentList.as_view(), name='student_list'),
chea
  • 101
  • 1
  • 11
0

The best and the simplest paginator that I have ever seen is uni_paginator

Disclaimer. I am author of it.

You can start using it, or dive into the code and see how it works. Be happy to answer any questions.

Here is the link

Good luck!

mrvol
  • 2,575
  • 18
  • 21