Next and Before Links for a django paginated query

前端 未结 4 709
小蘑菇
小蘑菇 2021-01-07 06:07

I\'m trying to make a search form for Django.

Its a typical search form and then returns a table of matches. I wish to paginate the tables returned.

The prob

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-07 07:09

    The below works before and after a search form has been submitted:

    Views.py

    class PostListView(ListView):
        model = Post #.objects.select_related().all()
        template_name = 'erf24/home.html'  # /_.html
        context_object_name = 'posts'      # default >> erf24/post_list.html
        ordering = ['date_posted']
        paginate_by = 3
    
    def is_valid_queryparam(param):
        return param != '' and param is not None
    def invalid_queryparam(param):
        return param == '' and param is None
    
    class SearchView(ListView):
        model = Post #.objects.select_related().all()
        template_name = 'erf24/home.html'  # /_.html
        context_object_name = 'posts'      # default >> erf24/post_list.html
        ordering = ['date_posted']
        paginate_by = 3
    
        def get_queryset(self): # new
            key = self.request.GET.get('key')
            minp = self.request.GET.get('min')
            maxp = self.request.GET.get('max')
    
            if is_valid_queryparam(key):
                obj = Post.objects.filter(Q(content__icontains=key) | Q(location__icontains=key)).distinct().order_by('date_posted')
    
            if is_valid_queryparam(minp):
                obj = Post.objects.filter(Q(price__gte=minp)).distinct().order_by('date_posted')
            if is_valid_queryparam(maxp):
                obj = Post.objects.filter(Q(price__lte=maxp)).distinct().order_by('date_posted')
    
            if is_valid_queryparam(minp) & is_valid_queryparam(maxp):
                obj = Post.objects.filter(Q(price__gte=minp) & Q(price__lte=maxp)).distinct().order_by('date_posted')
    
            if is_valid_queryparam(key) & is_valid_queryparam(minp) & is_valid_queryparam(maxp):
                obj = Post.objects.filter(Q(content__icontains=key) | Q(location__icontains=key)).distinct()
                obj = obj.filter(Q(price__gte=minp) & Q(price__lte=maxp)).order_by('date_posted')
    
            if invalid_queryparam(key) & invalid_queryparam(minp) & invalid_queryparam(maxp):
                obj = Post.objects.all()
    
            return obj
    

    url.py

    urlpatterns = [
        path('', PostListView.as_view(), name='erf24-home'),
        path('search/', SearchView.as_view(), name='erf24-search'),
    ]
    

    Home.html

    {% for post in posts %} {% endfor %} {% if is_paginated %} {% if page_obj.has_previous %} First Previous {% endif %} {% for num in page_obj.paginator.page_range %} {% if page_obj.number == num %} {{ num }} {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %} {{ num }} {% endif %} {% endfor %} {% if page_obj.has_next %} Next Last {% endif %} {% endif %}

    Worked like a charm :) Enjoy!

提交回复
热议问题