# views.py
def search(request):
if request.method == "GET": # 如果是翻页将会进入到get方式中
name = request.session.get('name')
# 进行数据库查询
content = Book.objects.filter(name=name)#这里返回的是多条数据
if content.exists():
paginator = Paginator(content, 5) # 每页显示5条
try:
num = request.GET.get('index', 1) # 页面连接,用于翻页
number = paginator.page(num)
except PageNotAnInteger:
number = paginator.page(1)
except EmptyPage:
number = paginator.page(paginator.num_pages)
return render(request,'first.html','paginator':'paginator')
return render(request,'first.html')
else:
name = request.POST.get('name')
request.session['name'] = name # POST 方式中用于接收前台数据并存入session
# 进行数据库查询
content = Book.objects.filter(name=name)#这里返回的是多条数据
if content.exists():
paginator = Paginator(content, 5) # 每页显示5条
try:
num = request.GET.get('index', 1) # 页面连接,用于翻页
number = paginator.page(num)
except PageNotAnInteger:
number = paginator.page(1)
except EmptyPage:
number = paginator.page(paginator.num_pages)
return render(request,'first.html','paginator':'paginator')
return render(request,'first.html')
<!--first.html-->
{% for item in page.object_list%}
{{item.name}}
{%endfor%}
<!--分页部分-->
<div>
<nav aria-label="Page navigation">
<ul class="pagination">
{% if page.has_previous %}
<li>
<a href="?index={{ page.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
{% else %}
<li class="disabled">
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
{% endif %}
{% for page_number in paginator.page_range %}
{% if page_number == page.number %}
<li class="active"><a href="#">{{ page_number }}</a></li>
{% else %}
<li><a href="?index={{ page_number }}">{{ page_number }}</a></li>
{% endif %}
{% endfor %}
{% if page.has_next %}
<li>
<a href="?index={{ page.next_page_number }}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
{% else %}
<li class="disabled">
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
{% endif %}
</ul>
</nav>
</div>