Search code examples
djangodjango-sessions

How does one enforce automatic logout due to inactivity in a Django application?


In my Django application, I would like for the user to be automatically logged out after 30 minutes of inactivity, so I used this setting in settings.py:

SESSION_COOKIE_AGE = 1800

However, using this setting logs the user out in 30 minutes regardless of activity. How does one enforce automatic logout due to inactivity in a Django application?


Solution

  • You could update the session of an user when he accesses your site. For example in a middleware, this force session to be set again.

    class ActivateUser(object):
        def process_request(self, request):
            if request.user.is_authenticated():
                request.session.modified = True