Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filtering and pagination
#1
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:
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">&laquo; 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 &raquo;</a>
        {% endif %}
    </span>
</div>
{% endblock %}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  BeautifulSoup pagination using href rhat398 1 2,397 Jun-30-2021, 10:55 AM
Last Post: snippsat
  Python beautifulsoup pagination error The61 5 3,452 Apr-09-2020, 09:17 PM
Last Post: Larz60+
  Pagination prejni 2 2,388 Nov-18-2019, 10:45 AM
Last Post: alekson
  Scrapy Javascript Pagination (next_page) nazmulfinance 2 3,019 Nov-18-2019, 01:01 AM
Last Post: nazmulfinance
  pagination for non standarded pages zarize 12 5,987 Sep-02-2019, 12:35 PM
Last Post: zarize
  Python - Scrapy Javascript Pagination (next_page) Baggelhsk95 3 9,979 Oct-08-2018, 01:20 PM
Last Post: stranac
  BeautifulSoup and pagination. Mike Ru 1 7,874 Sep-22-2017, 10:15 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020