Use the next session of about Django

A, Session concept

  • A cookie is a key value stored in the browser on the data, while the session is the key to save the data on the server side
  • using the session dependency cookie: the Session after use, data stored in a Cookie sessionid, the browser will request each time this data to the server, the server after receiving the sessionid, according to the request will identify the value Session's.

Two, Django uses in session

  • session key for data storage

 

 

 

  • sessionThe default data stored in a database table django project (table named: django_session), save the following format:

 

 

 Third, the data operation:

  • Write session key-value pairs in the format
makes request.session [ 'key'] = value
  • The key to read the value
request.session.get ( 'key', the default) 
# or 
makes request.session [ 'key']
  • Clear all session, delete the value in the storage part 
request.session.clear()
  • Clear session data, delete session in store entire data
request.session.flush()
  • Delete the specified key and value in the session, only to delete a key and the corresponding value in the store
del request.session['键']
  • Set session data valid time; if not set, the default expiration time of two weeks
request.session.set_expiry(value)
  1. If the value is an integer, the session data will expire in value seconds without activity.
  2. If the value is None, the session never expires.
  3. If the value is 0, then the Cookie user session will expire when the user's browser is closed.

Fourth, the following are examples:

# Texting interfaces 
DEF SMS_SEND (Request): 
    # HTTP: // localhost:? 8000 / duanxin / duanxin / SMS_SEND / Phone = 18,434,288,349 
    # 1 to obtain the phone number 
    Phone = request.GET.get ( 'Phone') 
    # 2 generates 6 bit codes 
    code aliyunsms.get_code = (. 6, False) 
    #. 3 to cache the Redis 
    # cache.set (Phone, code, 60) # 60s period 
    #print ( 'determines whether the cache:', cache.has_key (phone) ) 
    #Print ( 'Get Redis codes:', cache.get (Phone)) 

    # temporarily with the session handler 
    makes request.session [ 'Phone'] = code request.session.set_expiry (300) set an expiration # 5 min 
    print ( 'determines whether the cache:', request.session.get ( 'Phone')) 
    Print ( 'Get session codes:', request.session.get ( 'Phone')) 
    #. 4 texting 
    result = aliyunsms.send_sms(phone, code)
    
    the HttpResponse return (Result)

 
# text verification code verification
sms_check DEF (Request): 
    ? # / duanxin / sms_check / code = & Phone = XXX XXX 
    codes # 1 and the manually entered telephone 
    Phone = request.GET.get ( 'Phone') 
    code = request.GET.get ( 'code ') 
    # 2. Get saved in redis code 
    #Print (' cache contains: ', cache.has_key (Phone)) 
    #Print (' value: ', cache.get (Phone)) 
    #cache_code = cache. GET (Phone) 
    # acquired in the session code 
    Print ( 'value:', request.session.get ( 'Phone')) 
    cache_code = request.session.get ( 'Phone') 

    # 3. Analyzing 
    if code == cache_code : 
        return the HttpResponse (json.dumps ({ 'Result': 'the OK'})) 
    the else: 
        return the HttpResponse (json.dumps ({ 'Result': 'False'}))

  

Guess you like

Origin www.cnblogs.com/zmdComeOn/p/12120660.html