1 ajax
$.ajax({
url:'/test/',
method:'get/post',
contentType:'application/json',
1 分页器基本使用
详见终极使用
2 分页器终极用法
2.1 后端
def books_page(request):
current_num = int(request.GET.get('page_num', 1))
book_list = models.Books.objects.all()
paginator = Paginator(book_list, 20)
try:
page = paginator.page(current_num)
except Exception as e:
current_num = 1
page = paginator.page(current_num)
if paginator.num_pages > 11:
if current_num - 5 < 1:
page_range = range(1, 12)
elif current_num + 5 > paginator.num_pages:
page_range = range(paginator.num_pages - 10, paginator.num_pages + 1)
else:
page_range = range(current_num - 5, current_num + 6)
else:
page_range = paginator.page_range
return render(request, 'book_page.html', locals())
2.2 前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<script src="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script src="/static/jquery-3.3.1.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">图书列表展示</h3>
</div>
<div class="panel-body">
<table class="table table-striped">
<thead>
<tr>
<th>id</th>
<th>书名</th>
<th>价格</th>
<th>出版社</th>
</tr>
</thead>
<tbody>
{% for book in page.object_list %}
<tr>
<td>{{ book.id }}</td>
<td>{{ book.name }}</td>
<td>{{ book.price }}</td>
<td>{{ book.publish }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="text-center">
<nav aria-label="Page navigation">
<ul class="pagination">
{% if page.has_previous %}
<li>
<a href="/books_page/?page_num={{ page.previous_page_number }}"
aria-label="Previous"