In Djangou use cookie and session

First, session tracking

  1. We first need to understand what is the conversation! The conversation can be understood as a conversation between the client and the server in a session may contain multiple requests and responses, for example, you make a phone call to 10086, you are the client, the server is the 10086 service personnel, from both sides effective call the moment the session began, to either hang up the phone indicates the end of the session. During a call, you can send multiple requests to 10086, then this will be multiple requests in a single session. Client sends a request to a server a first start, the session began, until the client closed the end of the browser session.

  2. Multiple data requests in a painting of the share, which is counting session tracking

Two, cookie

  1. What is a cookie

    1. 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.

  2. The principle cookie

    1. 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.

  3. cookie and HTTP header

    1. A cookie is an HTTP request and response headers transfer client and server

    2. cookie: request header, the client sends to the server

    3. Format: cookie: A = A; B = B. Both multiple cookie separated by a semicolon

    4. set-cookie: header in response, the server sends to the client;

  4. cookie coverage

    1. 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

  1. Ctrl + Shift + del three keys to clear the page cache and cookie, the future of this operation you will use a lot.

  2. Get cookie

    1. 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
  3. Set cookie

    rep = Httpresponse(...)
    rep = render(request,''')
    
    rep.set_cookie(key,value,...)
    rep.set_signed_cookie(key,value,salt='加密盐',max_age=None,...)

    parameter

    1. key: Key

    2. value: value

    3. max_age = None, timeout

    4. expires = None, timeout

    5. path = '/', cookie domain name in force

    6. secure = False, https transmission

    7. httponly = False only http protocol transport, can not be acquired Javascript

     
      
  4. Delete cookie

    1.   
      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

四、session

 Session is a server-side technology, the use of this technology, you can create a separate session object for each user's browser to the server is running, due to the user's browser session is exclusive, so the user's browser when accessing web resources, can the respective data in each session when the user access other web resources to go to the server, and then remove the other web resources for the user service data from the respective user session in

Five, django operation session  

  1. Obtain value

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 ()

  2. Set value

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

  3. Delete value

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,

Djangou in the session configuration

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)

 

Guess you like

Origin www.cnblogs.com/wang-xing-hao/p/11272590.html