我正在尝试对django-filter的结果进行分页。
我已根据相关文档,我的观点
中的建议配置了视图/模板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})
我的模板
{% 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 %}
但我能看到的只是错误信息(没有autopaginate阻止工作)
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
请帮我弄清楚我错在哪里。关于django-filter的文档表明列表可以作为f.qs
获得,但它对我来说无效。
答案 0 :(得分:0)
尝试使用render
快捷方式而非render_to_response
呈现视图,以便request
对象在模板上下文中可用。
from django.shortcuts import render
def itemimages(request):
f = ItemFilter(request.GET, queryset=Item.objects.all())
return render(request, 'library/images.html', {'filter': f})