2015-09-01 76 views
0

我试图分页django-筛选结果。django-pagination分页django-筛选结果

  • 的Django 1.8.3
  • Django的过滤器0.11.0
  • Django的分页1.0.7

我已经相关文件中配置视图/模板,因为有人建议,我的看法

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过滤器的文档表明,该列表可以获得为f.qs,但它从来没有为我工作。

回答

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})