Django session stored to Redis database

Source: Internet
Author: User

The session is stored in the Redis database and needs to be configured in the setting

Django-redis Chinese Documents

http://django-redis-chs.readthedocs.io/zh_CN/latest/#cache-backend

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 =new) 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.

Original address: 79116271

Django session stored to Redis database

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.