I'm trying to paginate results of django-filter.
I have configured view/templates as it is suggested in related documentation, my views
class ItemFilter(django_filters.FilterSet):
class Meta:
template_name = 'library/items.html'
model = Item
fields = ['type','publishing','author','tags']
order_by = ['id']
def itemimages(request):
f = ItemFilter(request.GET, queryset=Item.objects.all())
return render_to_response('library/images.html', {'filter': f})
my template
{% extends 'library/base.html' %}
{% load pagination_tags %}
{% block title %}Library Index{% endblock %}
{% block body_block %}
{% load staticfiles %}
<ul class="rig columns-3">
{% autopaginate filter 2 as filter_list %}
{% for obj in filter_list %}
<li>
<a href="/library/{{ obj.id }}/">
<img src="{% static "/" %}{{ obj.cover.url }}"/>
<p>{{ obj.title }}<br/>{{ obj.national_title }}</p>
</a>
</li>
{% endfor %}
{% paginate %}
</ul>
{% endblock %}
{% block sidebar_block %}
<form action="" method="get">
{{ filter.form.as_p }}
<input type="submit"/>
</form>
{% endblock %}
but all I can see is error message (it works without autopaginate block)
KeyError at /library/
'request'
Request Method: GET
Request URL: http://localhost:8000/library/
Django Version: 1.8.3
Exception Type: KeyError
Exception Value:
'request'
Exception Location: /usr/lib64/python2.7/site-packages/django/template/context.py in __getitem__, line 71
Python Executable: /usr/bin/python2.7
Python Version: 2.7.5
Please help me to identify where I am wrong. Documentation on django-filter suggests that list can be obtained as f.qs
, but it never worked for me.
Try rendering your view with the render
shortcut instead of render_to_response
, so that the request
object is available in the template context.
from django.shortcuts import render
def itemimages(request):
f = ItemFilter(request.GET, queryset=Item.objects.all())
return render(request, 'library/images.html', {'filter': f})