Python Forum

Full Version: Filtering and pagination
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
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')
        response =
    except PageNotAnInteger:
        response =
    except EmptyPage:
        response =
    return render(request,'search/page.html',{'response': response})
{% 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 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 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 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 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 class="form-group col-sm-4 col-md-3">
        {{ response.material.label_tag }}
        {% render_field filter.form.material class="form-control" %}
      <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" %}
    <button type="submit" class="btn btn-primary">
      <span class="glyphicon glyphicon-search"></span> Search
    <a href="{% url 'containersearch' %}">
    <button type="submit" class="btn btn-secondary">
      <span class="glyphicon glyphicon-search"></span> Reset

<div class="container-fluid pt-3">
<table class="table table-bordered">
      <th>Area Easting</th>
      <th>Area Northing</th>
      <th>Context Number</th>
      <th>Specific Material</th>

{% for samples in response %}

  <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>
    <div class="btn-group" role="group" aria-label="Basic example">

  <a href="{% url 'editsamplesearch' %}" class="btn btn-primary" role="button">edit</a>
  <a href="{% url 'editsamplesearch' %}" class="btn btn-secondary" role="button">take out</a>
  <a href="{% url 'editsamplesearch' %}" class="btn btn-secondary" role="button">request</a>

{% empty %}
  <td colspan="5">No data</td>

{% 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 }}.

        {% if response.has_next %}
            <a href="?page={{ response.next_page_number }}">next</a>
            <a href="?page={{ response.paginator.num_pages }}">last &raquo;</a>
        {% endif %}
{% endblock %}