Django session and paging (instance description), django instance description

Source: Internet
Author: User

Django session and paging (instance description), django instance description

We have introduced cookies, which are mainly used for user logon and saving the user logon status. However, cookies are directly stored in the browser, which is less secure. Therefore, session functions are the same as cookies, the difference is that it is placed on the client, which is more secure than cookies. There is also paging, which is a common function.

I. session

1. Basic Syntax:

1. Set the Sessions value request. session ['session _ name'] = "admin" 2. Obtain the Sessions value session_name = request. session ["session_name"] session_name = request. session. get ("session_name") 3. Delete the Sessions value del request. session ["session_name"] request. session. flush () 4. Check whether to operate the session value if "session_name" is request. session:

2. parsing Diagram

3. Instance

1) views

def login(requset): if requset.method=="POST":  username=requset.POST.get("user")  password=requset.POST.get("pwd")  ret=models.UserInfo.objects.filter(username=username,password=password)  if ret:   requset.session["IS_LOGON"]=True   requset.session["USER"]=username   return redirect("/home/")  else:   return redirect("/login/") return render(requset,"login.html")def home(request): ret=request.session.get("IS_LOGON",None) if ret :  username=request.session.get("USER")  return render(request, "home.html",locals()) else:  return redirect("/login/")

2) template

<Form action = "/login/" method = "post" >{% csrf_token %} <p> name <input type = "text" name = "user"> </p> <p> password <input type = "password" name = "pwd"> </p> <input type = "submit"> </form>

Ii. Paging

1. view

From django. shortcuts import render, HttpResponse # Create your views here. from app01.models import * from django. core. paginator import Paginator, EmptyPage, PageNotAnIntegerdef index (request): ''' batch import data: Booklist = [] for I in range (100): Booklist. append (Book (title = "book" + str (I), price = 30 + I * I) Book. objects. use of the bulk_create (Booklist) ''''' page sharer: book_list = Book. objects. all () paginator = Paginator (book_list, 10) print ("count:", paginator. count) # print ("num_pages", paginator. num_pages) # print ("page_range", paginator. page_range) # page number list page1 = paginator. page (1) # page Object on page 1st for I in page1: # print (I) print (page1.object _ list) # page2 = paginator. page (2) print (page2.has _ next () # whether the next page has print (page2.next _ page_number () # print (page2.has _ previous ()) # Whether the previous page has print (page2.previous _ page_number () # The previous page number # throwing an error # page = paginator. page (12) # error: EmptyPage # page = paginator. page ("z") # error: PageNotAnInteger ''' book_list = Book. objects. all () paginator = Paginator (book_list, 10) page = request. GET. get ('page', 1) currentPage = int (page) try: print (page) book_list = paginator. page (page) Comment t PageNotAnInteger: book_list = paginator. page (1) Partition t EmptyPage: book_list = paginator. page (paginator. num_pages) return render (request, "index.html", {"book_list": book_list, "paginator": paginator, "currentPage": currentPage })

2. templates

<! DOCTYPE html> 

3. Expansion

Def index (request): book_list = Book. objects. all () paginator = Paginator (book_list, 15) page = request. GET. get ('page', 1) currentPage = int (page) # if there are too many pages, use another display method if paginator. num_pages> 30: if currentPage-5 <1: pageRange = range () elif currentPage + 5> paginator. num_pages: pageRange = range (currentPage-5, paginator. num_pages + 1) else: pageRange = range (currentPage-5, currentPage + 5) else: pageRange = paginator. page_range try: print (page) book_list = paginator. page (page) Comment t PageNotAnInteger: book_list = paginator. page (1) Partition t EmptyPage: book_list = paginator. page (paginator. num_pages) return render (request, "index.html", locals ())

The above django session and page (instance description) are all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support the house of friends.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.