django自带的user验证
1 from django.shortcuts import render,redirect 2 from django.contrib import auth 3 from django.core.paginator import Paginator,EmptyPage 4 from django.contrib.auth.models import User 5 from app01 import models 6 # Create your views here. 7 def login(request): 8 if request.method=='POST': 9 username=request.POST.get('username') 10 password=request.POST.get('password') 11 user=auth.authenticate(username=username,password=password) 12 if user: 13 auth.login(request,user) 14 return redirect('/index/') 15 if request.method=="GET": 16 return render(request,'login.html') 17 18 def index(request): 19 if not request.user.is_authenticated: 20 return redirect('/login/') 21 username=request.user.username 22 user_list=User.objects.all() 23 return render(request,'index.html',{'name':username,'user_list':user_list})
django 自带的 分页器
1 from django.shortcuts import render,redirect 2 from django.core.paginator import Paginator,EmptyPage 3 from app01 import models 4 # Create your views here. 5 6 def book(request): 7 ''' 8 学习分页器 9 :param request: 10 :return: 11 ''' 12 ### 批量插入数据### 13 ''' 14 book_list=[] 15 for i in range(100): 16 temp=models.Book(name='book%s'%(i),price=i*i) 17 book_list.append(temp) 18 models.Book.objects.bulk_create(book_list) 19 ''' 20 ###分页器的学习### 21 ''' 22 paginator=Paginator(book_list,per_page=10) 23 print("page_range:",paginator.page_range)#页面列表 24 print("count:",paginator.count)#数据总数 25 print("num_pages:",paginator.num_pages)#总页数 26 print( book_page_list=paginator.page(1)) 27 print(book_page_list.object_list,book_page_list.has_previous(),book_page_list.has_next(),book_page_list.previous_page_number(),book_page_list.next_page_number()) 28 return render(request,'book.html',locals()) 29 ''' 30 book_list=models.Book.objects.get_queryset().order_by('id') 31 current_page=int(request.GET.get('page',1)) 32 paginator=Paginator(book_list,3) 33 page_range=None 34 try: 35 book_page_list=paginator.page(current_page) 36 37 38 if paginator.num_pages>11: 39 if current_page-5<1: 40 page_range=range(1,12) 41 elif current_page+5>paginator.num_pages: 42 page_range=range(paginator.num_pages+1-11,paginator.num_pages+1) 43 else: 44 page_range=range(current_page-5,current_page+5+1) 45 else: 46 page_range=paginator.page_range 47 except EmptyPage: 48 book_list=paginator.page(1) 49 return render(request,'book.html',{'page_range':page_range,'book_page_list':book_page_list})
分页器的前端显示
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"> 7 </head> 8 <body> 9 <h2>this is book page.</h2> 10 <ul> 11 {% for book in book_page_list %} 12 <li>{{ book.name }}-----{{ book.price }}</li> 13 {% endfor %} 14 15 </ul> 16 <div> 17 <nav aria-label="Page navigation"> 18 <ul class="pagination"> 19 {% if book_page_list.has_previous %} 20 <li> 21 <a href="?page={{ book_page_list.previous_page_number }}" aria-label="Previous"> 22 <span aria-hidden="true">«</span> 23 </a> 24 </li> 25 {% else %} 26 <li class="disabled"> 27 <a href="#" aria-label="Previous"> 28 <span aria-hidden="true">«</span> 29 </a> 30 </li> 31 {% endif %} 32 33 {% for item in page_range %} 34 {% if current_page == item %} 35 <li class="active"><a href="?page={{ item }}">{{ item }}</a></li> 36 {% else %} 37 <li ><a href="?page={{ item }}">{{ item }}</a></li> 38 {% endif %} 39 {% endfor %} 40 {% if book_page_list.has_next %} 41 <li> 42 <a href="?page={{ book_page_list.next_page_number }}" aria-label="Next"> 43 <span aria-hidden="true">»</span> 44 </a> 45 </li> 46 {% else %} 47 <li class="disabled"> 48 <a href="#" aria-label="Next"> 49 <span aria-hidden="true">»</span> 50 </a> 51 </li> 52 {% endif %} 53 54 </ul> 55 </nav> 56 </div> 57 </body> 58 </html>