在使用 django 实现分页问题,采用的是class-based views
.
实例代码如下:
class IndexView(generic.ListView):
template_name = 'blog/index.html'
model = Article
paginate_by = 20
但是每当加入paginate_by
,就会出错,浏览器错误信息为:
EmptyPage at /blog/
That page number is less than 1
Request Method: GET
Request URL: http://localhost:8000/blog/
Django Version: 1.9.5
Exception Type: EmptyPage
Exception Value:
That page number is less than 1
Exception Location: /home/hujianxin/.pyenv/versions/django-web/lib/python3.5/site-packages/django/core/paginator.py in validate_number, line 38
Python Executable: /home/hujianxin/.pyenv/versions/django-web/bin/python
Python Version: 3.5.1
Python Path:
['/home/hujianxin/Programming/personal-site',
'/home/hujianxin/Dropbox/Programming/Spark/spark-1.6.1-bin-hadoop2.6/python',
'/home/hujianxin/Programming/personal-site',
'/home/hujianxin/.pyenv/versions/django-web/lib/python35.zip',
'/home/hujianxin/.pyenv/versions/django-web/lib/python3.5',
'/home/hujianxin/.pyenv/versions/django-web/lib/python3.5/plat-linux',
'/home/hujianxin/.pyenv/versions/django-web/lib/python3.5/lib-dynload',
'/home/hujianxin/.pyenv/versions/anaconda3-2.4.1/envs/django-web/lib/python3.5/site-packages/setuptools-20.7.0-py3.5.egg',
'/home/hujianxin/.pyenv/versions/django-web/lib/python3.5/site-packages']
Server time: Sun, 22 May 2016 05:13:56 +0000
还请有经验的大大帮忙看一下。
1
hujianxin OP 找到原因了,原因是我在 html 文件中,没有判断 page_obj 时候存在前一页,我就使用 page_obj.previous_page_number 了。
但是呢,我把这一行注释掉,还是出现同样的错误,然后吧这一行完全删除,就不会这样的错误了,看来我对 django 模板的注释还是不熟,回去自习查查。 |