A, a Cookie
#Cookies#def cook1 (request):#Print (Request. COOKIES) # View Cooke## Print (Request.get_signed_cookie (' K2 ', salt= ' UUU '))#rep = httpresponse (' OK ') # set Cook must set in response## Rep.set_cookie (' K1 ', 123) # Ordinary Cooke## Rep.set_signed_cookie (' K2 ', 666,salt= ' UUU ') # Encrypted cookie Salt Strict# ##Rep.set_cookie (' k999 ', 123,path= '/cooke1/') # Adding a path means it will only take effect under the current URL#Rep.set_cookie (' k888 ', 123)#return Rep#def cook2 (request):#Print (Request. COOKIES) # View Cooke#rep = httpresponse (' OK ')#return Rep
1. Obtaining Cookies:
1 request. cookies['key']2 request.get_signed_cookie (key, Default=raise_error, salt=", max_age=None)3 parameters:4 default: Defaults 5 Salt: Encryption salt 6 max_age: Background control Expiration Time
2. Set Cookies:
1Rep =HttpResponse (...) or rep = render (Request, ...)2 3 Rep.set_cookie (key,value,...)4Rep.set_signed_cookie (key,value,salt='Encrypted Salt',...)5 Parameters:6 Key , Keys7Value="', the value8Max_age=None, time-out9Expires=none, Time-out (IE requires expires, so set itifHasn't been already.)TenPath='/', the path to which the cookie takes effect,/represents the root path, Special: Cookie with path can be accessed by any URL of the page Onedomain=None, the domain name in which the cookie takes effect ASecure=False, HTTPS transport -Httponly=false can only be transmitted by HTTP protocol and cannot be obtained by JavaScript (not absolute, the underlying capture can be obtained or overwritten)
Because cookies are stored on the client's computer, JavaScript and jquery can also manipulate cookies.
1 <script src='/static/js/jquery.cookie.js'></script>2 $ . Cookie ("list_pager_num"'/' });
The session is supported by default in Sessiondjango, which provides 5 types of sessions for developers to use:
- Database (default)
- Cache
- File
- Cache + Database
- Encrypt cookies
1. Database session
Django supports session by default, and the session data is stored in the database by default, which is: Django_session table. A. Configuring settings.py session_engine='django.contrib.sessions.backends.db' #engine (default)session_cookie_name ="SessionID" #session's cookie is stored on the browser when the key, namely: Sessionid= random string (default)Session_cookie_path ="/" #The path of the session's cookie Save (default)Session_cookie_domain = None#Session cookie saved domain name (default)Session_cookie_secure = False#whether HTTPS transmits cookies (default)Session_cookie_httponly = True#whether the session's cookie only supports HTTP transport (default)Session_cookie_age = 1209600#session's cookie expiration date (2 weeks) (default)Session_expire_at_browser_close = False#whether to close the browser so that the session expires (default)Session_save_every_request = False#whether the session is saved every time the request is modified, and then saved (by default)B. UsedefIndex (Request):#get, set, delete data in sessionrequest.session['K1'] Request.session.get ('K1', None) request.session['K1'] = 123Request.session.setdefault ('K1', 123)#exists then does not set delrequest.session['K1'] #all keys, values, key-value pairsRequest.session.keys () request.session.values () Request.session.items () request.session.i Terkeys () request.session.itervalues () Request.session.iteritems ( )#random string for user sessionRequest.session.session_key#Delete all data with session expiration date less than current daterequest.session.clear_expired ()#Check if the random string in the user session is in the databaseRequest.session.exists ("Session_key") #Delete all session data for the current userRequest.session.delete ("Session_key") ...
2. Cache session
A. Configuring settings.py session_engine='Django.contrib.sessions.backends.cache' #engineSession_cache_alias ='default' #the cache alias used (the default memory cache, or memcache), where the alias relies on the cached settingssession_cookie_name ="SessionID" #session cookie is stored on the browser when the key, namely: Sessionid= random stringSession_cookie_path ="/" #The path of the session cookie saveSession_cookie_domain = None#session of the cookie saved by the domain nameSession_cookie_secure = False#whether HTTPS transmits cookiesSession_cookie_httponly = True#whether the session cookie only supports HTTP transmissionSession_cookie_age = 1209600#Cookie Expiration date for session (2 weeks)Session_expire_at_browser_close = False#whether to close the browser so that the session expiresSession_save_every_request = False#if the session is saved every time the request is modified, it is saved after the defaultB. Use of ibid.
3. File session
A. Configuring settings.py session_engine='Django.contrib.sessions.backends.file' #engineSession_file_path = None#cache file path, if none, use the Tempfile module to get a temporary address tempfile.gettempdir () # such as:/var/folders/d3/j9tj0gz93dg06bmwxmhh6_xm0000gn/tsession_cookie_name ="SessionID" #session cookie is stored on the browser when the key, namely: Sessionid= random stringSession_cookie_path ="/" #The path of the session cookie saveSession_cookie_domain = None#session of the cookie saved by the domain nameSession_cookie_secure = False#whether HTTPS transmits cookiesSession_cookie_httponly = True#whether the session cookie only supports HTTP transmissionSession_cookie_age = 1209600#Cookie Expiration date for session (2 weeks)Session_expire_at_browser_close = False#whether to close the browser so that the session expiresSession_save_every_request = False#if the session is saved every time the request is modified, it is saved after the defaultB. Use of ibid.
4. Cache + Database Session
the database is used for persistence, and the cache is used to improve the efficiency a. Configuration settings.py ' django.contrib.sessions.backends.cached_db ' # engine B. Use of ibid.
5. Encryption Cookie Session
A. Configuring settings.py 'django.contrib.sessions.backends.signed_cookies' # engine B. Use of ibid.
Extension: Session user authentication
def Login (func): def Wrap (Request, *args, * *Kwargs) :# If not logged in, jump to the specified page if ' /test/': return redirect ('/http www.baidu.com') return func (Request, *args, * *Kwargs) return Wrap
Python Django Session/cookie