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="?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="?page={{ contacts.next_page_number }}">next</a> {% endif %} </span> </div> </body> </html>
效果:
注:参考 https://docs.djangoproject.com/en/1.10/topics/pagination/