Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable sessions in Django

My site serves pages that are almost static and there are no users with logins etc.

I want to completely disable sessions for performance reasons (so django won't access the DB to get the session on every request).

I removed django.contrib.sessions from INSTALLED_APPS, is there anything else I need to do?

like image 969
Dan Bolofe Avatar asked Oct 19 '22 02:10

Dan Bolofe


1 Answers

You should locate articles how to set up sessions in django and remove everything from settings.py according to such article. Here is it:

Although this should already be setup and working correctly, it’s nevertheless good practice to learn which Django modules provide which functionality. In the case of sessions, Django provides middleware that implements session functionality.

To check that everything is in order, open your Django project’s settings.py file. Within the file, locate the MIDDLEWARE_CLASSES tuple. You should find the django.contrib.sessions.middleware.SessionMiddleware module listed as a string in the tuple - if you don’t, add it to the tuple now. It is the SessionMiddleware middleware which enables the creation of unique sessionid cookies.

The SessionMiddleware is designed to work flexibly with different ways to store session information. There are many approaches that can be taken - you could store everything in a file, in a database, or even in a cache. The most straightforward approach is to use the django.contrib.sessions application to store session information in a Django model/database (specifically, the model django.contrib.sessions.models.Session). To use this approach, you’ll also need to make sure that django.contrib.sessions is in the INSTALLED_APPS tuple of your Django project’s settings.py file. If you add the application now, you’ll need to synchronise your database using the python manage.py syncdb command to add the new tables to your database.

So it seems that you should remove middleware too. Maybe it's not necessary, but if you're using static application, then you it's not that bad to remove everything according to the sessions.

like image 80
Dracontis Avatar answered Oct 22 '22 00:10

Dracontis