Jun-14-2018, 08:11 PM
I have 50k+ records so I need to use pagination, also users need to be able to search through the queryset, I cannot get it to work. In the following code the pagination works but not the filtering, any ideas? The client needs it up and running on Monday!
views.py:
views.py:
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator from . import filters def listing(request): # BTW you do not need .all() after a .filter() # local_url.objects.filter(global_url__id=1) will do filtered_qs = filters.SampleFilter(request.GET,queryset=Samples.objects.all()).qs paginator = Paginator(filtered_qs, 15) page = request.GET.get('page') try: response = paginator.page(page) except PageNotAnInteger: response = paginator.page(1) except EmptyPage: response = paginator.page(paginator.num_pages) return render(request,'search/page.html',{'response': response})Template:
{% extends 'search/base.html' %} {% load widget_tweaks %} {% block content %} <form method="get"> <div class="well container"> <h4 style="margin-top: 0">Filter</h4> <div class="row"> <div class="form-group col-sm-4 col-md-3"> {{ filter.form.sample_id.label_tag }} {% render_field filter.form.sample_id class="form-control" %} </div> <div class="form-group col-sm-4 col-md-3"> {{ filter.form.area_easting.label_tag }} {% render_field filter.form.area_easting class="form-control" %} </div> <div class="form-group col-sm-4 col-md-3"> {{ filter.form.area_northing.label_tag }} {% render_field filter.form.area_northing class="form-control" %} </div> <div class="form-group col-sm-4 col-md-3"> {{ filter.form.context_number.label_tag }} {% render_field filter.form.context_number class="form-control" %} </div> <div class="form-group col-sm-4 col-md-3"> {{ filter.form.sample_number.label_tag }} {% render_field filter.form.sample_number class="form-control" %} </div> <div class="form-group col-sm-4 col-md-3"> {{ response.material.label_tag }} {% render_field filter.form.material class="form-control" %} </div> <div class="form-group col-sm-4 col-md-3"> {{ filter.form.specific_material.label_tag }} {% render_field filter.form.specific_material class="form-control" %} </div> </div> <button type="submit" class="btn btn-primary"> <span class="glyphicon glyphicon-search"></span> Search </button> <a href="{% url 'containersearch' %}"> <button type="submit" class="btn btn-secondary"> <span class="glyphicon glyphicon-search"></span> Reset </button> </a> </div> </form> <div class="container-fluid pt-3"> <table class="table table-bordered"> <thead> <tr> <th>Sample(id)</th> <th>Area Easting</th> <th>Area Northing</th> <th>Context Number</th> <th>Material</th> <th>Specific Material</th> <th>actions</th> </tr> </thead> <tbody> {% for samples in response %} <tr> <td>{{ samples.sample_id }}</td> <td>{{ samples.area_easting }}</td> <td>{{ samples.area_northing }}</td> <td>{{ samples.context_number }}</td> <td>{{ samples.sample_number }}</td> <td>{{ samples.material }}</td> <td> <div class="btn-group" role="group" aria-label="Basic example"> <a href="{% url 'editsamplesearch' pk=samples.pk %}" class="btn btn-primary" role="button">edit</a> <a href="{% url 'editsamplesearch' pk=samples.pk %}" class="btn btn-secondary" role="button">take out</a> <a href="{% url 'editsamplesearch' pk=samples.pk %}" class="btn btn-secondary" role="button">request</a> </div> </td> </tr> {% empty %} <tr> <td colspan="5">No data</td> </tr> {% endfor %} <div class="pagination"> <span class="step-links"> {% if response.has_previous %} <a href="?page=1">« first</a> <a href="?page={{ response.previous_page_number }}">previous</a> {% endif %} <span class="current"> Page {{ response.number }} of {{ response.paginator.num_pages }}. </span> {% if response.has_next %} <a href="?page={{ response.next_page_number }}">next</a> <a href="?page={{ response.paginator.num_pages }}">last »</a> {% endif %} </span> </div> {% endblock %}