首页 > 代码库 > django -- 内置分页
django -- 内置分页
urls.py
from django.conf.urls import url from conn_oracle import views urlpatterns = [ url(r‘^page/‘, views.page), ]
views.py
from django.shortcuts import render from conn_oracle import models from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger def page(request): contact_list = models.UserRegister.objects.all() paginator = Paginator(contact_list, 10) # Show 10 contacts per page page = request.GET.get(‘page‘) try: contacts = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. contacts = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. contacts = paginator.page(paginator.num_pages) return render(request, ‘page.html‘, {‘contacts‘: contacts})
models.py
from django.db import models class UserRegister(models.Model): user_id = models.CharField(max_length=200, primary_key=True, db_column=‘user_id‘, null=True) login_name = models.CharField(max_length=50, db_column=‘login_name‘, null=True) user_gender = models.CharField(max_length=1, db_column=‘user_gender‘) user_real_name = models.CharField(max_length=200) user_hsp_name = models.CharField(max_length=300) class Meta: db_table = ‘user_register‘
page.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1 class="i1">Info of USERS</h1> <table class="i1" border = 1px cellpadding="5"> <thead> <tr> <th>用户id</th> <th>用户名</th> <th>性别</th> <th>姓名</th> <th>单位</th> </tr> </thead> <tbody> {% for item in contacts %} <tr> <td>{{ item.user_id }}</td> <td>{{ item.login_name }}</td> <td>{{ item.user_gender }}</td> <td>{{ item.user_real_name }}</td> <td>{{ item.user_hsp_name }}</td> </tr> {% endfor %} </tbody> </table> <div class="pagination"> <span class="step-links"> {% if contacts.has_previous %} <a href="http://www.mamicode.com/?page={{ contacts.previous_page_number }}">previous</a> {% endif %} <span class="current"> Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}. </span> {% if contacts.has_next %} <a href="http://www.mamicode.com/?page={{ contacts.next_page_number }}">next</a> {% endif %} </span> </div> </body> </html>
效果:
注:参考 https://docs.djangoproject.com/en/1.10/topics/pagination/
django -- 内置分页
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。