Pager
Django provides paging module to facilitate time when the large amount of data, for distributed processing.
First use
Steps for usage
First, import
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
Second, instantiate
paginator = Paginator (<page object list> <entry per page number>) to generate a Paginator objects #
Third, the method using a page
page = paginator.page (<page number>): displays the corresponding page entry, and generate a page class
Outline
Django distribution module provided mainly uses two classes: Paginator and Page class, have their properties and methods
Paginator
The following common properties and methods of such introduction.
First, create a method
paginator = Paginator (<page object list> <entry per page number>) to generate a Paginator objects #
Second, common attributes
<Paginator Object> .count: the number of paging object list
<Paginator Object> .num_pages: Number of pages
<Paginator Object> .page_range: page range
Page
First, create a method
page = <paginator Object> .page (<page number>)
Second, the common method
<Page Objects> .next_page_number () # get on the next page
<Page objects> .previous_page_number () # Get Previous page number
<Page Objects> .has_next () # if a next page
<Page Objects> .has_previous () # if there Previous
Examples are as follows:
views
from django.shortcuts import render from django.http import HttpResponse from app01 import models from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger # Create your views here. def index(request): books = models.Book.objects.all() paginator = Paginator(books, 8) try: num = int(request.GET.get('p', 1)) # print(paginator.count) # print(paginator.num_pages) # print(paginator.page_range) page = paginator.page(num) except Exception as e: page = paginator.page(1) page_range = paginator.page_range # print(page.next_page_number()) # print(page.previous_page_number()) # print(page.has_next()) # print(page.has_previous()) return render(request, 'index.html', locals()) def add(request): books = [] for i in range(100): book = models.Book(name='Book' + str(i), price=30 + i * i) books.append(book) models.Book.objects.bulk_create(books) return HttpResponse('OK')
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/dist/css/bootstrap.css"> </head> <body> <ul> {% for book in page %} <li>{{ book.name }}</li> {% endfor %} </ul> <nav aria-label="Page navigation"> <ul class="pagination"> {% if page.has_previous %} <li> <a href="/?p={{ page.previous_page_number }}" aria-label="Previous"> Previous </a> </li> {% else %} <li class="disabled"> <a aria-label="Previous"> Previous </a> </li> {% endif %} {% for i in page_range %} {% if i == num %} <li class="active"><a href="/?p={{ i }}">{{ i }}</a></li> {% else %} <li><a href="/?p={{ i }}">{{ i }}</a></li> {% endif %} {% endfor %} {% if page.has_next %} <li> <a href="/?p={{ page.next_page_number }}" aria-label="Next"> Next Page </a> </li> {% else %} <li class="disabled"> <a href="#" aria-label="Next"> Next Page </a> </li> {% endif %} {# <li>#} {# <a href="/?p={{ page.next_page_number }}" aria-label="Next">#} {# 下一页#} {# </a>#} {# </li>#} </ul> </nav> </body> </html>
Authentication system ---- Cookie and Session
Before ordination
A user logs on when Taobao, will be in the "shopping cart" to see the record A shopping. B log in, see their records. Smart servers seem to be able to identify and remember the user, the user returns the corresponding data, and do not need to enter each time you connect a user name and password. But Http protocol is a stateless protocol, it can not save the information about the user. Then the above mentioned Taobao landing is how to achieve? Cookie and Session This requires two things.
Cookie
What is a Cookie? This is a container in the client browser storage. What store do? When you enter a user name and password in the login page, click submit, will take Cookie in the past, the server authentication. If verified, the server returns a key-value pair, the key is the sessionID, and the value is randomly generated string string. Then this key pair to the client browser, the client browser put this key to save a Cookie. Next time you access the server, then access with Cookie. Cookie server is to be verified. Cookie addition to put login information, you can also put other personal information to the server for processing.
Session
What is Session? Is actually stored on the server side of a container, this is what store it? Cookie has been said above, the browser sends the user name and password, to verify if the server is generated by the key-value pair returned to the client. But the second time over the browser with Cookie, the server is how to verify? In fact, the first landing, the server will generate a random string of Cookie stored in the session. In this way, when the future browser access, you can put the value of sessionID Cookie browser and key in Session brought in to be compared to, for verification. This Session will be held in django_session default database, the field contains session_id, session_data, the expiration time.
Note: For the same server, the same browser saves only the information a user. Go on the web site on which cookie use.
General Certification Process
Logout
Write-off is very simple, is to record data in the table django_session session cleared it. The following statement can be realized.
request.session.flush()