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 show using the function based views method.
https://simpleisbetterthancomplex.com/tutorial/2016/08/03/how-to-paginate-with-django.html
My updated users_list.html is the following:
{% extends 'base.html' %}
{% load widget_tweaks %}
{% block content %}
<form method="get">
<div class="well">
<h4 style="margin-top: 0">Filter</h4>
<div class="row">
<div class="form-group col-sm-4 col-md-4">
<label/> 3-4 User ID
{% render_field filter.form.employeentname class="form-control" %}
</div>
<div class="form-group col-sm-4 col-md-4">
<label/> First Name
{% render_field filter.form.employeefirstname class="form-control" %}
</div>
<div class="form-group col-sm-4 col-md-4">
<label/> Last Name
{% render_field filter.form.employeelastname class="form-control" %}
</div>
<div class="form-group col-sm-4 col-md-4">
<label/> Status
{% render_field filter.form.statusid class="form-control" %}
</div>
<div class="form-group col-sm-4 col-md-4">
<label/> Title
{% render_field filter.form.positiondesc class="form-control" %}
</div>
</div>
<button type="submit" class="btn btn-primary">
<span class="glyphicon glyphicon-search"></span> Search
</button>
</div>
</form>
<form action = "{% url 'results' %}" form method = "POST">
{% csrf_token %}
<table class="table table-bordered">
{{ form.as_table }}
<thead>
<tr>
<th>3-4</th>
<th>First name</th>
<th>Last name</th>
<th>Title</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
{% for user in filter.qs %}
<tr>
<td>{{ user.employeentname }}</td>
<td>{{ user.employeefirstname }}</td>
<td>{{ user.employeelastname }}</td>
<td>{{ user.positiondesc }}</td>
<td>{{ user.statusid }}</td>
<td><input type="checkbox" name="usercheck" value = "{{user.employeentname}}" /> </td>
</tr>
{% empty %}
<tr>
<td colspan="5">No data</td>
</tr>
{% endfor %}
</tbody>
</table>
<button class="btn btn-primary" type="submit">Select User</button>
</form>
{% endblock %}
My view below is based on the tutorial, but i'm using my own model:
def user_list(request):
user_list = Employee.objects.all()
user_filter = UserFilter(request.GET, queryset=user_list)
user_list = user_filter.qs
page = request.GET.get('page', 1)
paginator = Paginator(user_list.qs, 10)
try:
users = paginator.page(page)
except PageNotAnInteger:
users = paginator.page(1)
except EmptyPage:
users = paginator.page(paginator.num_pages)
args = {'filter':user_filter, 'users':users}
return render(request, 'user_list.html', args)
My directory structure is setup as the following, perhaps its something with that:
from django import forms
from .models import Employee
import django_filters
class UserFilter(django_filters.FilterSet):
class Meta:
model = Employee
fields = ['employeentname', 'employeefirstname', 'employeelastname', 'statusid', 'positiondesc']
def __init__(self, *args, **kwargs):
super(UserFilter, self).__init__(*args, **kwargs)
# at sturtup user doen't push Submit button, and QueryDict (in data) is empty
if self.data == {}:
self.queryset = self.queryset.none()
Added my base.html after moving pagination there:
{% load static %}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<style type="text/css">
.page-header {
margin-top: 0;
}
</style>
</head>
<body>
{% include 'includes/header.html' %}
<div class="container">
{% block content %}
{% if users.has_other_pages %}
<ul class="pagination">
{% if users.has_previous %}
<li><a href="?page={{ users.previous_page_number }}">«</a></li>
{% else %}
<li class="disabled"><span>«</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 }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
{% endblock %}
</div>
<script src="{% static 'js/jquery-3.1.1.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
{% block javascript %}
{% endblock %}
</body>
</html>
What i'm expecting is for my filter to paginate the search results for every 10 users. What i'm getting now is the entire set of users. The filter works correctly, it's the pagination that isn't.
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" /> </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 }}">«</a></li>
{% else %}
<li class="disabled"><span>«</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 }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>