Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django session key changing upon authentication

I have a Django app which records users' product choices for both authenticated users. My intention is to use the request.session.session_key variable to associate anonymous data with a user if they decide to register later, a la this post:

Django storing anonymous user data

However, it seems that the session key changes when the user logs in/ registers so the session key can no longer be associated with the user. Is this the correct behaviour of the Django session framework. Is there a solid way to achieve the functionality I'm looking for?

Any help much appreciated.

like image 558
Darwin Tech Avatar asked Dec 20 '12 19:12

Darwin Tech


People also ask

How do I save a session in Django?

To change this default behavior, set the SESSION_SAVE_EVERY_REQUEST setting to True . When set to True , Django will save the session to the database on every single request. Note that the session cookie is only sent when a session has been created or modified.

How does Django authentication work?

The Django authentication system handles both authentication and authorization. Briefly, authentication verifies a user is who they claim to be, and authorization determines what an authenticated user is allowed to do. Here the term authentication is used to refer to both tasks.

How is session managed in Django?

Django uses a cookie containing a special session id to identify each browser and its associated session with the site. The actual session data is stored in the site database by default (this is more secure than storing the data in a cookie, where they are more vulnerable to malicious users).


1 Answers

In settings.py

SESSION_ENGINE = 'youapp.session_backend'

in directory youapp in file session_backend.py

from django.contrib.sessions.backends.db import SessionStore as DbSessionStore

class SessionStore(DbSessionStore):
    def cycle_key(self):
        pass

And session not changed after login

like image 56
nnmware Avatar answered Oct 13 '22 01:10

nnmware