django framework of the session cookie

cookie
because http is a stateless protocol, user operation can not step on the record, so it is necessary to maintain state.
The difference between the cookie and session:
1.cookie is stored locally in the browser, so relatively safe. A cookie is a size of 4k, save up to 20 (the most primitive because the local browser is very small, just a historical data, but the fact is inaccurate), the default expiration time is 14 days.
2.session is stored on the server, and is relatively safe, session depends on the cookie, sessionid stored in a cookie, theoretically unlimited size.
3. The same point: keep the change is realized like, are all server-generated, time has expired, you can define your own time expired.

1. Set the cookie
in response to which set a cookie
HttpResponse.set_cookie (cookie name, value = cookie value, max_age = cookie is valid)

 

 

2. Read the cookie

Cookie value can be read by this request carries COOKIES HttpRequest object attributes. request.COOKIES for the dictionary type.

 

 session
session two ways of understanding the need to understand
the broad sense: session mechanism - session, for the relationship between the record http request many times, is the relationship between the state data
in the narrow sense: session data - saving mechanism for session state data.
1. Enable session
Django is enabled by default in the session, in setting the middleware already set, if you disable it, uninstall or comments.

 

 2. storage
2.1 database storage
flask in the session cookie is placed in default, that is, the browser
SESSION_ENGINE = 'django.contrib.sessions.backends.db'
default storage method is stored in the database
if stored in a database also you need to install the app session configuration iNSTALLED-APPS

 

 

 in a database table, you can see a table django_session

2.2 local cache
stored in the machine's memory, you can not recover if you lose, than the way the database to read and write faster.
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
save a local problem:
Cross-machine multi-machine access in case of problems

Multiple servers keep a code reason:
In order to ensure concurrency
QPS: the number of requests per second
to prevent downtime: the code is not the problem can be solved, use the Sentinel redis mechanism inside.

2.3 hybrid storage
priority access from the machine's memory, if there is no access from the database.
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'

2.4redis
1. Installation extension
pip install django-redis
introduced directly as this may be extended to the rear end session using the
Configuration
arranged in setting.py the following
essentially increases the local cache based on a configuration of the rear end ,

 

 Read and write property of the session through the session object HttpRequest.
1) key-value pair format to write session.
makes request.session [ 'key'] = value
2) according to the key value is read.
request.session.get ( 'key', default)
3) remove all the session, delete the value in the storage section.
request.session.clear ()
. 4) clear session data, delete the entire session in the data store.
request.session.flush ()
. 5) deletes the specified value and the session key, the delete key and only a corresponding value in the store.
del request.session [ 'key']
6) disposed session Validity
request.session.set_expiry (value)
• If the value is an integer, session expires after no activity value seconds.
• If the value is 0, then the user's session Cookie will expire when the user's browser is closed.
• If the value is None, then the session is valid uses the system default, which two weeks, you can set SESSION_COOKIE_AGE to set the global default value in the settings.py. (In seconds)

Guess you like

Origin www.cnblogs.com/shengguorui/p/11571796.html