Python learning day13 Django paging/session/cookie

Source: Internet
Author: User

Created on May 3, 2017


Lesson 1th: Introduction to work on the previous section

Put the mouse on the form and enter the editing mode------Learn----cmdb
Modal dialog box---Popup dialog---------to the background
Way:
1 form-full refresh with Submit page
2 control modal dialogs with Ajax background requests, return data by Sessuss mode


Lesson 2nd: Contents of this section 1
Lesson 3rd: Contents of this section 2

Lesson 4th: Django Custom Paging 28minutes
Lesson 5th: Django Custom Paging two 35minutes

URL (r ' ^index/(\d*) ', Views.index),
index/(\d*) There is no argument behind here.

HTML returned to the front end in the background, displayed as a string in the front end, and not displayed directly on the page as HTML
From django.utils.safestring import Mark_safe
Available via Mark_safe (' HTML tag ')
Divmod (12,5)

def index (request,page):
Try
page = Int (page)
Except
page = 1
Per_item = 5
Start = (page-1) *per_item
End = Page*per_item
Count = Asset.objects.all (). Count ()
result = Asset.objects.all () [Start:end]

temp = Divmod (count, Per_item)
If temp[1] = = 0:
All_page_count = temp[0]
Else
All_page_count = temp[0]+1
page_html = "<a href= '/app01/index/%d ' > Home </a>"% (1)
For I in Range (1,all_page_count+1):
a_html = "<a href= '/app01/index/%d ' >%d</a>"% (i,i)
page_html + = a_html

page_html + = "<a href= '/app01/index/%d ' > Last </a>"% (All_page_count)
page = Mark_safe (page_html)

ret = {' data ': result, ' count ': Count, ' page ':p age}
Return Render_to_response (' app01/index.html ', ret)

Lesson 6th: Django Custom Paging three 35minutes


Lesson 7th Django Custom Distribution four 40minutes

Good HTML page-out code:
#-*-Coding:utf-8-*-

From django.utils.safestring import Mark_safe
‘‘‘
Created on May 4, 2017
This is a paging module that can be paginated for all data
@author: Administrator
‘‘‘
#这里需要提供: Current page, quantity per page, models displayed
def model (PAGE,PER_ITEM,M):
Try
page = Int (page)
Except
page = 1
Start = (page-1) *per_item
End = Page*per_item
Count = M.objects.all (). Count ()
result = M.objects.all () [Start:end]
temp = Divmod (count, Per_item)
If temp[1] = = 0:
All_page_count = temp[0]
Else
All_page_count = temp[0]+1
Return All_page_count,result,count

#这里需要提供: Current page, total pages, URL of the turn
Def Pager (Page,all_page_count,url):
Try
page = Int (page)
Except
page = 1
#判断如果页面是1, the previous page is none, the normal previous page is currently less than 1
If page <= 1:
page_html = "<a href= ' # > Prev </a>"
Else
page_html = "<a href= '%s/%d ' > Prev </a>"% (url,page-1)
#首页就是第1页
page_html + = "<a href= '%s/%d ' > Home </a>"% (url,1)

#页面显示11个页码, if the current page is less than 5, the start page is 1, the normal Start page is the current minus 5
If page-5 <=0:
Page_start = 1
Page_end = 12
Else
Page_start = page-5
If All_page_count-6>=page:
Page_end = page+6
Else
Page_end = All_page_count

For I in Range (page_start,page_end+1):
If page = = I:
a_html = "<a class= ' selected ' href= '%s/%d ' >%d</a>"% (url,i,i)
Else
a_html = "<a href= '%s/%d ' >%d</a>"% (url,i,i)
page_html + = a_html

page_html + = "<a href= '%s/%d ' > Last </a>"% (Url,all_page_count)
#
If page < all_page_count:
page_html + = "<a href= '%s/%d ' > Next </a>"% (url,page+1)
Else
page_html + = "<a href= ' # ' > Next </a>"
page = Mark_safe (page_html)
Return page

8th Session Anatomy and Application 45minutes
Cookie
exists on the client's browser

session
exists on the server, keeping the session (for example, after logging on to other pages)

--------- --------------Sesson-----------------------------------------
def index (request):
# Here to determine whether there is a Request.sesson value, return to the normal page, no return to the login page
User_dict = request.session.get (' Is_login ', None)
if user_dict:
Return Render_to_response (' app02/index.html ', {' user ': user_dict[' user ']})
Else:
return redirect ('/app02/ Login ')


DEF login (Request):
ret = {' status ': None}
if Request.method = = ' POST ':
Username = Request. Post.get (' username ', None)
Password = Request. Post.get (' password ', None)
Print Username,password
If username = = ' aaa ' and password = = ' 111111 ':
#这里给Session设定一个值, ture If you are logged in as normal
# request.session[' is_login ') = True
request.session[' is_login ' = {' user ': username}
ret[' status ' = ' login successful '
Return redirect ('/app02/index ')
Else
ret[' status ' = ' User name or password error '
Return Render_to_response (' app02/login.html ', ret)

def logout (Request):
#删除sesson里的值, you can use Del.
Del request.session[' Is_login ']
Return redirect ('/app02/index ')

---------------------------------------------------------------------

Request.session This information all methods can be used

request.session[' is_login ' = {' user ': username}

The 9th session of Django in the running mechanism 28minutes

Session related parameters are set in setting

If the cookie is disabled in the client browser, the website cannot write the relevant data locally and cannot log on to the Web site

The relationship between Cookiet and session:
Each user is logged in to generate a cookie that, when the user visits the site, takes the value of the cookie, and the site retains a
The key of the session (the value of the cookie) to confirm whether it is the user.
Multiple users multiple key


The 9th Lesson Django Analysis and Application 33minutes

Manually set a cookie for a page
Response = Render_to_response (' app02/login.html ', ret)
Response.set_cookie (' K1 ', ' v1 ')

Jquery.cookie.js has jquery's cookie operation

Python learning day13 Django paging/session/cookie

Related Article

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.