环境:
django-pagination 1.0.7
Python Version: 2.7.11
Django Version: 1.5.12
在Python3.51+Django1.92下没测试成功。
一、下载并安装django-pagination 1.0.7
二、修改settings.py:
INSTALLED_APPS = (
# ...
'pagination',
)
MIDDLEWARE_CLASSES = (
# ...
'pagination.middleware.PaginationMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request",
)
修改views.py,增加:
from django.template.context import RequestContext
def index(request):
record = conn_mongodb.mongodata()
return render_to_response("index.html", {"record": record}, context_instance = RequestContext(request))
修改对应的html页面,这里是index.html,如下:
{% load pagination_tags %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>产品列表</title>
<style> a{ text-decoration:none} </style>
</head>
<body bgcolor="#f1f1f1">
{% autopaginate record 10 %}
<table border="0" width="80%" align="center" cellpadding="10" cellspacing="20" bgcolor="#f1f1f1">
<tr>
{% for n in record %}
<td width="220px"> <div class="p-img"> <a href={{n.link}}> <img width="220" height="220" src={{n.pic}}> {{n.name}} </a> </div></
td>
{% if forloop.counter|divisibleby:"4" %}
</tr> <br/> <tr>
{% endif %}
{% endfor %}
<td> </td>
</tr>
</table>
<br/><br/>
<center> {% paginate %} </center>
</body>
templates/pagination 下把安装的pagination.html拷到此处
并修改如下:
{% if is_paginated %}
{% load i18n %}
<div class="pagination">
{% if page_obj.has_previous %}
<a href="?page={{ page_obj.previous_page_number }}{{ getvars }}{{ hashtag }}" class="prev"> {% trans "上一页" %}</a>
{% else %}
<span class="disabled prev"> {% trans "上一页" %}</span>
{% endif %}
{% for page in pages %}
{% if page %}
{% ifequal page page_obj.number %}
<span class="current page">{{ page }}</span>
{% else %}
<a href="?page={{ page }}{{ getvars }}{{ hashtag }}" class="page">{{ page }}</a>
{% endifequal %}
{% else %}
...
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}{{ getvars }}{{ hashtag }}" class="next">{% trans "下一页" %} </a>
{% else %}
<span class="disabled next">{% trans "下一页" %} </span>
{% endif %}
</div>
{% endif %}
评论