I.Django内置分页
指令:
""" per_page: 每页显示条目数量 count: 数据总个数 num_pages:总页数 page_range:总页数的索引范围,如: (1,10),(1,200) page: page对象 has_next 是否有下一页 next_page_number 下一页页码 has_previous 是否有上一页 previous_page_number 上一页页码 object_list 分页之后的数据列表 number 当前页 paginator paginator对象 """
插入数据:
""" #插数据 for i in range(100): n = 'root'+str(i) models.Books.objects.create(title=n) #插数据(首选) book_list = [] #[<Books: Books object>.....] for i in range(100): book_list.append(models.Books(title="book%s" % i)) models.Books.objects.bulk_create(book_list) """
代码示例:
urls.py
url(r'^index/',views.index),
views.py
from django.core.paginator import Paginator,Page,PageNotAnInteger,EmptyPage def index(request): """ 分页 :param request: :return: """ # for i in range(300): #插数据 # n='root'+str(i) # models.Boy.objects.create(name=n) current_page=request.GET.get('page') boy_list=models.Boy.objects.all() paginator =Paginator(boy_list,10) try: posts=paginator.page(current_page) except PageNotAnInteger as e: posts=paginator.page(1) except EmptyPage as e: posts=paginator.page(1) return render(request,'in.html',{'posts':posts})
in.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>内置分页</title> </head> <body> <h1>用户列表</h1> <ul style="list-style: none"> {% for f in posts.object_list %} <li>{{ f.name }}</li> {% endfor %} </ul> <div> {% if posts.has_previous %} <a href="/index?page={{ posts.previous_page_number }}">上一页</a> {% endif %} {% for num in posts.paginator.page_range %} <a href="/index?page={{ num }}">{{ num }}</a> {% endfor %} {% if posts.has_next %} <a href="/index?page={{ posts.next_page_number }}">下一页</a> {% endif %} </div> </body> </html>
#注意点:Django内置分页 不适合 页码显示,适合 上一页和下一页。(若用,会显示所有页码)
II.Django 自定义分页
urls.py
url(r'^custom/',views.custom),
views.py
from utils.pager import PageInfo def custom(request): all_count = models.Boy.objects.all().count() page_info = PageInfo(request.GET.get('page'),all_count,10,'/custom',11) boy_list = models.Boy.objects.all()[page_info.start():page_info.end()] return render(request,'custom.html',locals())
custom.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>自定义分页</title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css"> <style> </style> </head> <body> <h1>用户列表</h1> <ul> {% for row in boy_list %} <li>{{ row.name }}</li> {% endfor %} </ul> <nav aria-label="Page navigation"> <ul class="pagination "> {{ page_info.pager|safe }} </ul> </nav> </body> </html>
pager.py
class PageInfo(object): def __init__(self,current_page,all_count,per_page,base_url,show_page=11): """ :param current_page: :param all_count:数据总行数 :param per_page: :param base_url: :param show_page: """ try: self.current_page=int(current_page) except Exception as e: self.current_page = 1 self.per_page = per_page a,b = divmod(all_count,per_page) if b: a=a+1 self.all_pager = a self.show_page = show_page self.base_url = base_url def start(self): return (self.current_page-1) * self.per_page def end(self): return self.current_page * self.per_page def pager(self): page_list = [] half = int((self.show_page-1)/2) #如果数据总页数<11 if self.all_pager < self.show_page: begin=1 stop = self.all_pager + 1 #如果数据总页数 > 11 else: #数据当前页 <=5,永远显示1,11 if self.current_page <= half: begin = 1 stop = self.show_page + 1 else: if self.current_page + half > self.all_pager: begin = self.all_pager - self.show_page + 1 stop = self.all_pager + 1 else: begin = self.current_page - half stop = self.current_page + half + 1 if self.current_page <= 1: prev = '<li><a href="#">上一页</a></li>' else: prev="<li><a href='%s?page=%s'>上一页</a></li>"%(self.base_url,self.current_page-1) page_list.append(prev) for i in range(begin,stop): if i==self.current_page: temp = '<li class="active"><a href="%s?page=%s">%s</a></li>'%(self.base_url,i,i,) else: temp="<li><a href='%s?page=%s'>%s</a></li>"%(self.base_url,i,i,) page_list.append(temp) if self.current_page >= self.all_pager: nex="<li><a href='#'>下一页</a></li>" else: nex="<li><a href='%s?page=%s'>下一页</a></li>"%(self.base_url,self.current_page+1,) page_list.append(nex) return ''.join(page_list)
III.Django 自定义分页
#-------更多的使用模块
urls.py
url(r'^$',views.books), #使用的根目录
views.py
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger def books(req): book_list_all=models.Books.objects.all() p_obj = Paginator(book_list_all,20) #--------------------p_obj print(p_obj.count) #100 print(p_obj.num_pages) #5 print(p_obj.page_range) #!!! #------------------------------- page_num = req.GET.get('page') try: book_list = p_obj.page(page_num) except EmptyPage: book_list = p_obj.page(p_obj.num_pages) except PageNotAnInteger: book_list = p_obj.page(1) return render(req,'show.html',locals())
show.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>分页显示</title> <link rel="stylesheet" href="/static/bootstrap-3.3.5-dist/css/bootstrap.css"> </head> <body> <ul> {% for book in book_list %} <li>{{ book.title }}</li> {% endfor %} </ul> <ul class="pagination"> {% if book_list.has_previous %} <li><a href="/?page={{ book_list.previous_page_number }}">上一页</a></li> {% else %} <li class="disabled"><a href="#">上一页</a></li> {% endif %} {% for num in p_obj.page_range %} <li><a href="/?page={{ num }}">{{ num }}</a></li> {% endfor %} {% if book_list.has_next %} <li><a href="/?page={{ book_list.next_page_number }}">下一页</a></li> {% else %} <li class="disabled"><a href="#">下一页</a></li> {% endif %} </ul> </body> </html>
1.简单登录
view.py
def login(request): if request.method == 'GET': return render(request,'login.html') else: u = request.POST.get('user') p = request.POST.get('pwd') if u=='alex' and p=='123': request.session['username']='alex' request.session['email'] = 'alex3714@qq.com' return redirect('/index/') else: return render(request,'login.html',{'msg':'用户名或密码错误'}) def index(request): return HttpResponse('登录成功')
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form method="POST" action="/login/"> {% csrf_token %} <input type="text" name="user" /> <input type="text" name="pwd" /> <input type="submit"value="提交" />{{ msg }} </form> </body> </html>