Django分页不起作用

时间:2016-02-11 04:27:33

标签: python django pagination

按照教程我已经完成了2次,现在分页不起作用 我收到以下错误

TypeError at /posts/
object of type 'method' has no len()
Request Method: GET
Request URL:    http://127.0.0.1:8000/posts/
Django Version: 1.9.2
Exception Type: TypeError
Exception Value:    
object of type 'method' has no len()
Exception Location: /Users/ray/Desktop/uply/my-server/lib/python3.5/site-    packages/django/core/paginator.py in _get_count, line 77
Python Executable:  /Users/ray/Desktop/uply/my-server/bin/python
Python Version: 3.5.1
Python Path:    
['/Users/ray/Desktop/uply/my-server/src',
'/Users/ray/Desktop/uply/my-server/lib/python35.zip',
'/Users/ray/Desktop/uply/my-server/lib/python3.5',
'/Users/ray/Desktop/uply/my-server/lib/python3.5/plat-darwin',
'/Users/ray/Desktop/uply/my-server/lib/python3.5/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5',
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin',
'/Users/ray/Desktop/uply/my-server/lib/python3.5/site-packages']
Server time:    Wed, 10 Feb 2016 23:25:54 -0500

这是我在views.py中的代码

def post_list(request):
  queryset_list = Post.objects.all
  paginator = Paginator(queryset_list, 25)

  page = request.GET.get('page')
  try:
    queryset = paginator.page(page)
  except PageNotAnInteger:
    queryset = paginator.page(1)
  except EmptyPage:
    queryset = paginator.page(paginator.num_pages)

  template = "posts/post_list.html"
  name = "user"
  context = {
    "object_list": queryset,
    "name": name
  }
  return render(request, template, context)

在过去的两个月中,我使用相同的最新版本的django和pyhthon完成了本教程和各种变体。到底是怎么回事?怎么可能是这个问题      'method'类型的对象没有len()

我之前做过这个?欢迎大家帮忙。

2 个答案:

答案 0 :(得分:1)

all是一种方法:

queryset_list = Post.objects.all()

答案 1 :(得分:0)

Post.objects.all 更改为 Post.objects.all()

def post_list(request):
  queryset_list = Post.objects.all()
  paginator = Paginator(queryset_list, 25)

  page = request.GET.get('page')
  try:
    queryset = paginator.page(page)
  except PageNotAnInteger:
    queryset = paginator.page(1)
  except EmptyPage:
    queryset = paginator.page(paginator.num_pages)

  template = "posts/post_list.html"
  name = "user"
  context = {
    "object_list": queryset,
    "name": name
  }
  return render(request, template, context)