**
Django筛选数据分类遇到只能跳转第一页,第二页无数据的情况。
**
1.models.py创建模型
class User(models.Model):
uname= models.CharField(max_length=20)
uEmail= models.CharField(max_length=35)
uphone= models.CharField(max_length=15)
2.views.py从数据库中筛选数据
if request.method == 'POST':
name = request.POST.get('code')
else:
name = request.GET.get('code')
UserList = User.objects.filter(uphone=name).order_by()
3.分页器
paginator = Paginator(UserList,2) # 3 posts in each page
page = request.GET.get('page')
try:
pagedata = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
pagedata = paginator.page(1)
except EmptyPage:
# # If page is out of range deliver last page of results
pagedata = paginator.page(paginator.num_pages)
4.传递数据
return (render(request,'Qt15606/students.html',{'name':name,'page': pagedata,'pagedata': pagedata}))
5.前端界面students.html
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
<a style="font-weight:bold;color:#383957;" href="?page=1&code={{name}}" >第一页</a>
<a class="btn btn-primary btn-lg active" href="?page={{page.previous_page_number}}&code={{name}}">上一页</a>
{% endif %}
<span style="font-weight:bold;color:#383957" class="current">
第{{ page.number}}页 ··· 共 {{ page.paginator.num_pages}}页
</span>
{% if page.has_next %}
<a class="btn btn-primary btn-lg active" href="?page={{page.next_page_number }}&code={{name}}">下一页</a>
<a class="btn btn-primary btn-lg active" href="?page={{page.paginator.num_pages}}&code={{name}}">最后一页</a>
{% endif %}
</span>
</div>
其中最重要的一步是要在链接上加筛选的条件。href="?page={{page.next_page_number }}&code={{name}}"