django-通過使用者id取相同表的暱稱-分頁功能
阿新 • • 發佈:2018-12-16
分頁功能
#新增分頁器 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger @login_required() def index(request,page): wzalllist=Wzbiao.objects.all().order_by('-wzrq') #下面數字為每頁顯示幾條資料 paged=Paginator(wzalllist,2) try: page=int(request.GET.get('page',1)) wzalllist=paged.page(page) except: wzalllist = paged.page(1) content = {} content['wzalllist'] = wzalllist #locals()表示傳遞所有變數 return render(request, 'index.html', locals())
<div > <ul > {% if wzalllist.has_previous %} <li ><a href="?page={{ wzalllist.previous_page_number }}{% if request.GET.year %}&year={{ request.GET.year }}{% endif %}{% if request.GET.month %}&month={{ request.GET.month }}{% endif %}{% if request.GET.cid %}&cid={{ request.GET.cid }}{% endif %}">«上一頁</a></li> {% else %} <li >«上一頁</li> {% endif %} <li >{{ wzalllist.number }}/{{ wzalllist.paginator.num_pages }}</li> {% if wzalllist.has_next %} <li ><a href="?page={{ wzalllist.next_page_number }}{% if request.GET.year %}&year={{ request.GET.year }}{% endif %}{% if request.GET.month %}&month={{ request.GET.month }}{% endif %}{% if request.GET.cid %}&cid={{ request.GET.cid }}{% endif %}">下一頁 »</a></li> {% else %} <li >下一頁 »</li> {% endif %} </ul> </div>
上面需要替換wzalllist為傳遞的列表