Where are Django sessions stored?

I have this code in my project that assigns a unique identifier to an anonymous user and stores it in a session:

user_id = str(uuid.uuid4())[:5] request.session['nonuserid'] = user_id 

The documentation says that the sessions are stored in my database. I thought he would save it in the django_session table. However, every time a unique function is created and stored in a session (above the code), not a single row is added to this table.

Then I checked the cookies in Resources . There is no key named nonusedid . Just some sessionid

So where are the session data that I created stored?

Relevant part of Settings.py

 MIDDLEWARE_CLASSES = ( # Default Django middleware. 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', ) DJANGO_APPS = ( # Default Django apps: 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ) SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db' 
+5
source share
3 answers

I thought he would save it in the django_session table. However, each time unique and created and saved in the session (above the code), not a single row is added to this table.

We hope that a new line is not added every time you add a key / value to the current session. Session data is stored in serialized form (in the django_session.session_data field), and a new line is added only when a new session starts - all subsequent entries in the session will only update the contents of the session_data field.

+4
source

The only things to use for django sessions mentioned by you and other users are:

1- Add SessionMiddleware to MiddlewareClasses (you already have):

 MIDDLEWARE_CLASSES = ( # Default Django middleware. 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', ) 

2- Add django.contrib.sessions to installed applications (you already have):

 DJANGO_APPS = ( # Default Django apps: 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ) 

3- Database synchronization after changing middleware and DjangoApps:

  python manage.py syncdb 

Now that you have done all this, the sessions should work because they are no more. After you make all these changes, try to start searching for your website with or without different users, and you should get some information growing in the django_session table

You should check the session documentation in Django using sessions in views

+1
source

If you want to use a database-enabled session, you need to add "django.contrib.sessions" to the INSTALLED_APPS setting.

Once you have configured your installation, run the manage.py migrate command to install a separate database table that stores the session data.
as indicated here

0
source

Source: https://habr.com/ru/post/987555/


All Articles