Paginating the results of a Django forms POST request

前端 未结 7 445
旧巷少年郎
旧巷少年郎 2020-12-04 13:39

I\'m using Django Forms to do a filtered/faceted search via POST, and I would like to Django\'s paginator class to organize the results. How do I preserve the original requ

相关标签:
7条回答
  • 2020-12-04 14:27

    I did this in my web application with get parameters Maybe i can help you :

    Views.py

    class HomeView(ListView):
    model = Hotel
    template_name = 'index.html'
    paginate_by = 10  # if pagination is desired
    
    def get_queryset(self):
       qs = super().get_queryset()
       kwargs = {}
       if 'title' in self.request.GET:
           title = self.request.GET.get('title')
           if title != '':
               kwargs['title__icontains'] = title
       if 'category' in self.request.GET:
           category = self.request.GET.get('category')
           if category:
               kwargs['category_id'] = category
       if 'size' in self.request.GET:
           size = self.request.GET.get('size')
           if size:
               kwargs['size_id'] = size
       if 'service' in self.request.GET:
           service = self.request.GET.get('service')
           if service:
               kwargs['service_id'] = service
       if 'ownership' in self.request.GET:
           ownership = self.request.GET.get('ownership')
           if ownership:
               kwargs['ownership_id'] = ownership
       qs = qs.filter(**kwargs)
       return qs
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        form_init = {}
        form = SearchForm()
        if self.request.GET.items():
            try:
                parameters = self.request.GET.items()
            except KeyError:
                parameters = {}
            for key, value in parameters:
                for field in form.fields:
                    if key == field:
                        form_init[key] = value
            form.initial = form_init
        if 'title' in self.request.GET:
           title = self.request.GET.get('title')
           if title != '':
               context.update({
                'title': title
               })
        if 'category' in self.request.GET:
           category = self.request.GET.get('category')
           context.update({
            'category': category
           })
        if 'size' in self.request.GET:
           size = self.request.GET.get('size')
           context.update({
               'size': size
          })
        if 'service' in self.request.GET:
           service = self.request.GET.get('service')
           context.update({
               'service': service
          })
        if 'ownership' in self.request.GET:
           ownership = self.request.GET.get('ownership')
           context.update({
              'ownership': ownership
           })
        context.update({
            'search_form': form
        })
        return context
    

    Pagination file html

    <div class="row">
      {% if is_paginated %}
      <nav aria-label="...">
        <ul class="pagination">
          {% if page_obj.has_previous %}
            <li class="page-item"><a class="page-link" href="?category={{category}}&size={{size}}&service={{service}}&ownership={{ownership}}&page={{ page_obj.previous_page_number }}">Previous</a></li>
          {% else %}
            <li class="page-item disabled"><span class="page-link">Previous</span></li>
          {% endif %}
          <span class="page-current">
                   Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
               </span>
          {% if page_obj.has_next %}
            <li class="page-item"><a class="page-link" href="?category={{category}}&size={{size}}&service={{service}}&ownership={{ownership}}&page={{ page_obj.next_page_number }}">Next</a></li>
          {% else %}
            <li class="page-item disabled"><span class="page-link">Next</span></li>
          {% endif %}
        </ul>
      </nav>
     {% endif %}
    </div>
    
    0 讨论(0)
提交回复
热议问题