Django Filter with Pagination

前端 未结 6 1267
梦谈多话
梦谈多话 2020-12-19 12:47

I\'m attempting to follow the following tutorial for pagination with django filters, but the tutorial seems to be missing something, and i\'m unable to get the pagination to

相关标签:
6条回答
  • 2020-12-19 13:05

    I have written a post on implementing pagination with Django with function based view on my blog, you can check here

    I have discussed several ways to implement pagination in Django

    0 讨论(0)
  • 2020-12-19 13:10

    You can paginate like this:

    Note: user_filter.qs has filtered results and user_filter.queryset has unfiltered results

    views.py

          def search(request):
             user_list = Employee.objects.all()
             user_filter = UserFilter(request.GET, queryset=user_list)
             user_list = user_filter.qs
    
    
             paginator = Paginator(user_list, 10)
             page = request.GET.get('page', 1)
             try:
                users = paginator.page(page)
             except PageNotAnInteger:
                users = paginator.page(1)
             except EmptyPage:
                users = paginator.page(paginator.num_pages)
             args = {'paginator': paginator,'filter':user_filter, 
               'users':users,}
             return render(request, 'search/user_list.html', args)
    

    And then in the template:

       {% for user in users %}
         <tr>
          <td>{{ user.employeeusername }}</td>
          <td>{{ user.employeefirstname }}</td>
          <td>{{ user.employeelastname }}</td>
          <td>{{ user.statusid }}</td>
          <td><input type="checkbox" name="usercheck" />&nbsp;</td>
    
        </tr>
      {% empty %}
        <tr>
          <td colspan="5">No data</td>
        </tr>
      {% endfor %}
    </tbody>
    

     {% if users.has_other_pages %}
       <ul class="pagination">
        {% if users.has_previous %}
          <li><a href="?page={{ users.previous_page_number }}">&laquo;</a></li>
        {% else %}
         <li class="disabled"><span>&laquo;</span></li>
      {% endif %}
        {% for i in users.paginator.page_range %}
          {% if users.number == i %}
            <li class="active"><span>{{ i }} <span class="sr-only">(current)
         </span></span></li>
          {% else %}
            <li><a href="?page={{ i }}">{{ i }}</a></li>
          {% endif %}
        {% endfor %}
        {% if users.has_next %}
          <li><a href="?page={{ users.next_page_number }}">&raquo;</a></li>
        {% else %}
          <li class="disabled"><span>&raquo;</span></li>
        {% endif %}
      </ul>
    
    0 讨论(0)
  • 2020-12-19 13:11

    It's working fine for me. And if still, it's not working out for you, It's better to save time and try other alternatives.

    Here are some suggestions:

    1.Please try Q objects

    2.You can do a lot with kwargs

    3.Django REST Framework Filtering

    4.For search-engine like capability haystack is a beast - maybe a bit complicated for a newbie.

    5.You can also use Elastic Search.

    Views.py

    def avail_list(request):
            avails1 = AvailstaticCopy.objects.all().order_by('-row_date')
            avail_filter = AvailFilter(request.GET, queryset=avails1)
            avails1 = avail_filter.qs
    
    
            paginator = Paginator(avails1, 144)
            page = request.GET.get('page',1)
    
            try:
                users = paginator.page(page)
            except PageNotAnInteger:
                users = paginator.page(1)
            except EmptyPage:
                users = paginator.page(paginator.num_pages)
    
    
            context = {
                'paginator': paginator,
                'users': users,
                'filter': avail_filter,
            }
            return render(request,'avails/avail_list.html',context)
    

    A Portion of the template for reference with a situation similar to the question:

         <div class="row">
            <div class="form-group col-sm-4 col-md-3">
    
              {{ filter.form.row_date.label_tag }}
              {% render_field filter.form.row_date class="form-control" %}
    
            </div>
    
            <div class="form-group col-sm-4 col-md-3">
              {{ filter.form.director.label_tag }}
              {% render_field filter.form.director class="form-control" %}
            </div>
            <div class="form-group col-sm-8 col-md-6">
              {{ filter.form.manager.label_tag }}
              {% render_field filter.form.manager class="form-control" %}
            </div> 
            <div class="form-group col-sm-8 col-md-6">
              {{ filter.form.analyst.label_tag }}
              {% render_field filter.form.analyst class="form-control" %}
            </div> 
            <div class="form-group col-sm-8 col-md-6">
            <button type="submit" class="btn btn-primary">
            <span class="glyphicon glyphicon-search"></span> Search
            </button>
            </div>
            </div>
          </div>
    
        </div>
    
    
      <table class="table table-bordered" >
        <thead>
          <tr>
    
            <th>row_date</th>
            <th>Director</th>
            <th>Manager</th>
            <th>Analyst</th>
    
          </tr>
        </thead>
        <tbody>
          {% for a in users %}
            <tr>
              <td>{{ a.row_date }}</td>
              <td>{{ a.director }}</td>
              <td>{{ a.manager }}</td>
              <td>{{ a.analyst }}</td>
            </tr>
            {% empty %}
            <tr>
              <td colspan="5">No data</td>
            </tr>
            {% endfor %}
          </tbody>
          </table>
        </div>
           {% block javascript %}
        <script src="{% static 'search/js/jquery-3.1.1.min.js' %}"></script>
        <script src="{% static 'search/js/bootstrap.min.js' %}"></script>
    
         {% endblock %}
      </body>
    </html>
    
    0 讨论(0)
  • 2020-12-19 13:17
    • Simple & sweet,
    • use this, pip install filter-and-pagination
    • https://pypi.org/project/filter-and-pagination/

    Implementation Step

    1. install package by pip install filter-and-pagination
    2. import FilterPagination by from filter_and_pagination import FilterPagination in view.py
    3. in your function writte code as bellow standards...
    queryset = FilterPagination.filter_and_pagination(request, Customer)
    serialize_data = CustomerSerializer(queryset['queryset'], many=True).data
    resultset = {'dataset': serialize_data, 'pagination': queryset['pagination']}
    
    • in this code Customer is Django model &
    • CustomerSerializer is a DRF Serializer class
    1. in the resultset it contains dataset & pagination data, In this format (API Response) link: https://github.com/ashish1997it/filter-pagination-dj#demo
    2. For the API request follow PostMan collection link: https://github.com/ashish1997it/filter-pagination-dj#postman in the header section it will take a parameter & request you customize as per your requirement

    If you still face any difficulty then contact me :)

    0 讨论(0)
  • 2020-12-19 13:19

    I have tested it right now!It works fine for me.Just sharing a portion of the template with a view like the previous answer

          <div class="row">
            <div class="form-group col-sm-4 col-md-3">
    
              {{ filter.form.row_date.label_tag }}
              {% render_field filter.form.row_date class="form-control" %}
    
            </div>
    
            <div class="form-group col-sm-4 col-md-3">
              {{ filter.form.director.label_tag }}
              {% render_field filter.form.director class="form-control" %}
            </div>
            <div class="form-group col-sm-8 col-md-6">
              {{ filter.form.manager.label_tag }}
              {% render_field filter.form.manager class="form-control" %}
            </div> 
            <div class="form-group col-sm-8 col-md-6">
              {{ filter.form.analyst.label_tag }}
              {% render_field filter.form.analyst class="form-control" %}
            </div> 
            <div class="form-group col-sm-8 col-md-6">
            <button type="submit" class="btn btn-primary">
            <span class="glyphicon glyphicon-search"></span> Search
            </button>
            </div>
            </div>
          </div>
    
        </div>
      </form>
    
      <table class="table table-bordered" >
        <thead>
          <tr>
    
            <th>row_date</th>
            <th>Director</th>
            <th>Manager</th>
            <th>Analyst</th>
    
          </tr>
        </thead>
        <tbody>
          {% for a in users %}
            <tr>
              <td>{{ a.row_date }}</td>
              <td>{{ a.director }}</td>
              <td>{{ a.manager }}</td>
              <td>{{ a.analyst }}</td>
    
              <td colspan="5">No data</td>
            </tr>
            {% empty %}
            <tr>
              <td colspan="5">No data</td>
            </tr>
            {% endfor %}
          </tbody>
          </table>
        </div>
           {% block javascript %}
        <script src="{% static 'search/js/jquery-3.1.1.min.js' %}"></script>
        <script src="{% static 'search/js/bootstrap.min.js' %}"></script>
    
         {% endblock %}
      </body>
    </html>
    
    0 讨论(0)
  • 2020-12-19 13:21

    I don't know if this is relevant but it seems to me that the issue may be where you are setting the paginator. In your view you have

    paginator = Paginator(user_list.qs, 10)
    

    but if you look closely at the solutions provided they are suggesting that you should be using

    paginator = Paginator(user_list, 10) 
    

    ie without .qs as you've already defined user_list in a previous line with user_list = user_filter.qs

    0 讨论(0)
提交回复
热议问题