Another hole in the Python+django, ask the students in Python class to come and see

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.