This is the way things are.
When using django1.6+, the default session is stored in the Database django_session table.
If you want to put the session in memory, you should configure it in the settings.py
Session_engine = "Django.contrib.sessions.backends.cache"
When used:
Added: request.session["userName"] = "Tom"
Query: name = request.session["UserName"]
Delete: Del request.session["UserName"]
Well, the more complicated data is how to store it: like an object student.
According to other language habits, such as Java and C #, it should be that:
Stu = Student (name= "Tom", age = 12)
Added: request.session["user"] = Stu
Query: Stu = request.session["User"]
Delete: del request.session["user"]
When storing objects, it is not a problem if the session is stored in memory. But once stored in the database is not.
Why?
Because of what is stored in the database, it is necessary to be able to do JSON serialization.
Student cannot be serialized as JSON. Need to convert student into a dictionary, yes, a dictionary.
{"Name": "Tom", "Age": 12}
This can be stored in the database session.
Also, if Stu is an object. Then execute the following statement:
Print ("1111111")
request.session["user"] = Stu
Print ("2222222")
Output is
111111
222222
This shows that the new operation of the session is multi-threaded, and does not block 222222 of the output, but in the HTTP request, the Stu can not be serialized by JSON error. Pit point, be careful.
exchange of professional, technical issues, etc. plus Python AC Group:338985564
Original Address: http://www.maiziedu.com/group/article/8361/?yy=tb0408
Another hole in the Python+django, ask the students in Python class to come and see