**
**
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 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 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}}"