33👍
As the author of those methods, I can see that the documentation isn’t very clear regarding this. Your observations are correct: only requests which cause the session to be altered is considered “activity”.
You can use the SESSION_SAVE_EVERY_REQUEST
setting to get the behavior you’re after (at the obvious cost of the session having to being saved every request).
Note : It will update the existing session record with latest expiry date.
4👍
A simple middleware would probably do better than setting this up in every view. This is what I used.
class SessionExpiry(object):
""" Set the session expiry according to settings """
def process_request(self, request):
if getattr(settings, 'SESSION_EXPIRY', None):
request.session.set_expiry(settings.SESSION_EXPIRY)
return None
This depends on SESSION_EXPIRY
being set in your config. It’s format is the same as request.session.set_expiry
.
MIDDLEWARE_CLASSES
should be defined with this order in mind:
MIDDLEWARE_CLASSES = (
...
'django.contrib.sessions.middleware.SessionMiddleware',
'<yourproject>.<yourapp>.middleware.SessionExpiry',
...
}
It’d be nice if django.contrib.sessions
took this setting into account by default.
- How does this Man-In-The-Middle attack work?
- How can I easily convert a Django app from mySQL to PostgreSQL?
- Django/Python – Check a date is in current week
- How to specify uniqueness for a tuple of field in a Django model
- Django form dropdown list of stored models
0👍
If you set "True" to SESSION_SAVE_EVERY_REQUEST on "settings.py" as shown below, automatically, session is updated every time the current page is reopened or other page is opened in Django Website.
SESSION_SAVE_EVERY_REQUEST = True # False by default
For example, session expires in 15 minutes. Then, from 3:00 pm, a session starts by logging in the page in Django Website so the session expires at 3:15 pm. Then, at 3:10 pm, the current page is reopened or other page is opened in Django Website so the session is updated so the new session expires at 3:25 pm which means you are logged in until 3:25 pm, so in other words, if the current page is not reopened or other page is not opened in Django Website then the new session expires at 3:25 pm which means you are logged out at 3:25 pm so you need to log in again to the page in Django Website to start a new session.
- How to properly query a ManyToManyField for all the objects in a list (or another ManyToManyField)?
- Advanced Django Template Logic