分页
from django.core.paginator import Paginator
if request.method=="GET":
cate=Cate.objects.all()
page_count=3 #每页显示多少条
# computer=Computer.objects.all() #获取所有数据
current_page=int(request.GET.get('p',1)) #当前页
pag=Paginator(cate,page_count) #数据显示多少条
goods_list=pag.get_page(current_page) #当前页的内容
total_page=pag.num_pages #总页数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/web/css/jquery.pagination.css">
<script type="text/javascript" src="/static/web/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/static/web/js/jquery.pagination.min.js"></script>
</head>
<body>
<ul>
{% load my_filter %} #过滤器
{% for i in goods_list %}
<li>{{ i.name|my_str }}</li>
{% endfor %}
</ul>
<div id="pagination"></div>
<input type="text" id="paginate"> #输入第几页点击跳转
<button type="button" onclick="tiao()">确定</button>
</body>
<script>
$("#pagination").pagination({
currentPage: {{current_page}},
totalPage: {{total_page}},
callback: function(current) {
window.location.href="/index/?p="+current
}
});
function tiao(){
var pp=$('#paginate').val()
window.location.href="/index/?p="+pp
}
</script>
</html>
过滤器:
在子应用下建templatetags文件夹
在文件夹下建my_filter.py文件
from django import template
register = template.Library()
@register.filter
def my_str(val):
return str(val) +'电脑'
{% load my_filter %} #过滤器
{% for i in goods_list %}
<li>{{ i.name|my_str }}</li>
{% endfor %}