-
-
Multiple data requests in a painting of the share, which is counting session tracking
Two, cookie
-
What is a cookie
-
First speaking, cookie is a browser technology, cookie specifically referring to was a little information, it is the server sends out a bundle of keys stored on the browser, it can be understood as the server to the client a small dessert, under It will automatically carry these keys to access the server when the time for the server to extract useful information.
-
-
The principle cookie
-
Works cookie is: a browser to access the server, with an empty cookie, and then generate content from the server, the browser receives the response stored locally; when the browser visits, the browser will automatically bring the cookie, so that the server can be judged by the content of this cookie who it is.
-
-
cookie and HTTP header
-
A cookie is an HTTP request and response headers transfer client and server
-
cookie: request header, the client sends to the server
-
Format:
cookie
: A = A; B = B. Both multiple cookie separated by a semicolon -
set-cookie: header in response, the server sends to the client;
-
-
cookie coverage
-
If the server then send repeated cookie will overwrite the old cookie, for example, a first client request for a cookie sent by the server is set-cookie: a = A; a second request is sent by the server: set-cookie: a = AA, then the client, leaving only a cookie, namely: a = AA
Three, django cookie operation
-
Ctrl + Shift + del three keys to clear the page cache and cookie, the future of this operation you will use a lot.
-
Get cookie
-
Request.Cookies [ ' Key ' ] request.get_signed_cookie (Key, default = RAISE_ERROR, = Salt '' , the max_age = Non
default: Default
salt: Salt encryption
max_age: background control expiration -
rep = Httpresponse(...) rep = render(request,''') rep.set_cookie(key,value,...) rep.set_signed_cookie(key,value,salt='加密盐',max_age=None,...)
-
key: Key
-
value: value
-
max_age = None, timeout
-
expires = None, timeout
-
path = '/', cookie domain name in force
-
secure = False, https transmission
-
httponly = False only http protocol transport, can not be acquired Javascript
-
-
DEF the Login (Request): REP = redrect ( " / the Login / " ) rep.delete_cookie ( " the User " ) # delete usercookie values previously set on the user's browser to return REP
makes request.session [ ' K1 ' ] request.session.get ( ' K1 ' , none) # makes request.session phrase from the cookie is to help you value taken out inside the sessionid,
the value django-session table corresponding to the inside of the sessionid data session-data field of that record in the you out (and decrypted), get a method to remove the key corresponding to the value of k1 to view all the keys, the key-value pair request.session.keys () Request. session.values () request.session.items ()
makes request.session [ ' K1 ' ] = 123 request.session.setdefault ( ' K1 ' , 123) # not is added, it has the same # help you generate random string, this will help you random strings and user data ( after encryption) and expiration time to save the django-session table inside,
to help you this random string sessionid: string added to the random way inside the cookie back to the browser, sessionid this name can be changed # pay attention to a thing , django-session table, you can not orm to control, because your models.py which do not correspond to the table
del makes request.session [ ' K1 ' ] # Django-sync remove the session table inside # Delete the current session data and deletes the session cookie. request.session.flush () # common, clear all cookie --- delete the session table to record the conversation,
Django default session supported therein provides five types of session for developers to use 1 . Database the Session SESSION_ENGINE = ' django.contrib.sessions.backends.db ' # engine (default) 2 . Caching the Session SESSION_ENGINE = ' django.contrib .sessions.backends.cache ' # engine SESSION_CACHE_ALIAS = ' default ' # cache alias used (the default cache memory, may be memcache), provided by the alias cache dependency 3 . file the Session SESSION_ENGINE = ' django.contrib.sessions. backends.file ' # engine SESSION_FILE_PATH = None # Cache file path, if None, using tempfile module obtains a temporary address tempfile.gettempdir () 4. + cache database SESSION_ENGINE = ' django.contrib.sessions.backends.cached_db ' # engine 5 . Encrypt the Session cookies SESSION_ENGINE = ' django.contrib.sessions.backends.signed_cookies ' # engine other public settings: SESSION_COOKIE_NAME = " sessionid " # the Session's cookie when a key on the browser, namely: sessionid = random string (the default) SESSION_COOKIE_PATH = " / " # the Session saved cookie path (default) SESSION_COOKIE_DOMAIN = None # Session of the preservation of cookie domain (default) SESSION_COOKIE_SECURE = False # whether Https transfer cookie (default) SESSION_COOKIE_HTTPONLY = True # whether the Session cookie only supports http transmission (default) SESSION_COOKIE_AGE = 1209600 # Session of cookie expiration date (2 week) (default) SESSION_EXPIRE_AT_BROWSER_CLOSE is = False # if you close the browser makes Session expired (default) SESSION_SAVE_EVERY_REQUEST = False # whether each request save Session, was saved after modifying default (default)