Reputation: 13753
I have this code in my project that assigns a unique id to an anomymous user and saves it in a session:
user_id = str(uuid.uuid4())[:5]
request.session['nonuserid'] = user_id
Documentation says that sessions are stored in my database. I thought it would save it in django_session
table. However, everytime a unique is created and saved in session(above code), no row is added to that table.
Then I checked cookies in Resources
. There is no key with name nonusedid
. Just some sessionid
So, where does it store the session data I created?
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'
Upvotes: 11
Views: 8060
Reputation: 1
The doc says below in Technical details:
- Session data is stored in a database table named
django_session
And, the default session id sessionid
is stored as a cookie in a browser as shown below:
In addition, with SESSION_COOKIE_NAME, you can change the default session id sessionid
to other names like hello
in settings.py
as shown below:
# "settings.py"
SESSION_COOKIE_NAME = 'hello'
Upvotes: 0
Reputation: 77912
I thought it would save it in django_session table. However, everytime a unique is created and saved in session(above code), no row is added to that table.
Hopefully a new row is not added each time you add a key/value to the current session. Session's data are stored in serialized form (in the django_session.session_data
field), and a new row is added only when a new session is started - all subsequent writes to the session will only update the session_data
field's content.
Upvotes: 10
Reputation: 10563
The only needs to use sessions in django as mentioned by you and other users are:
1- Add SessionMiddleware
to MiddlewareClasses(You already have it):
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 apps (You already have it):
DJANGO_APPS = (
# Default Django apps:
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
3- Sync the database after changing the Middleware and the DjangoApps:
python manage.py syncdb
Now that you have done all of this the sessions has to be working, because there is no more beyond that. After you do all this changes try to surf for your website logging with different users or without user and you should be some information growing up in the django_session
table
You should check the documentation about sessions at Django using sessions in views
Upvotes: 0
Reputation: 3526
If you want to use a database-backed session, you need to add 'django.contrib.sessions' to your INSTALLED_APPS setting.
Once you have configured your installation, run manage.py migrate to install the single database table that stores session data.
as specified here
Upvotes: 0