Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set session never expire in django

I am using Django-Python. Is there any way to set session never expire?

like image 226
user2307087 Avatar asked Mar 25 '15 07:03

user2307087


People also ask

How do I remove expired sessions in Django?

Django does not provide automatic purging of expired sessions. Therefore, it's your job to purge expired sessions on a regular basis. Django provides a clean-up management command for this purpose: clearsessions . It's recommended to call this command on a regular basis, for example as a daily cron job.

How are sessions maintained 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).

How long does a Django session last?

What is the default session timeout in Django? The setting you are looking for is SESSION_COOKIE_AGE , the default value is 1209600 which is two weeks, in seconds.


2 Answers

As of Django 1.4 (and later), you can also set the duration of a session by setting SESSION_COOKIE_AGE in the settings file.

SESSION_COOKIE_AGE

Default: 1209600 (2 weeks, in seconds)

The age of session cookies, in seconds.

like image 198
miceno Avatar answered Sep 19 '22 15:09

miceno


I don't know about infinite sessions, but if you use your session middleware and use a huge AUTOLOGOUT time, it will work.

does 100 years sound ok?

on settings.py

# time in minutes
# 60min*24hours*365days*100years
AUTO_LOGOUT = 52560000

Create a middleware and add it to settings.py in the middleware section

from datetime import datetime, timedelta
from django.conf import settings
from django.contrib import auth
from django.utils.translation import ugettext as _

class AutoLogout:

    def process_request(self, request):

        if not request.user.is_authenticated():
            # Can't log out if is not logged in
            return
        try:
            # last click
            last_touch = datetime.strptime(request.session['last_touch'], "%Y-%m-%d %H:%M:%S.%f")
            # getting auto logout time
            auto_logout_time = settings.AUTO_LOGOUT

            try:
                if datetime.now() - last_touch > timedelta(0, auto_logout_time * 60, 0):
                    del request.session['last_touch']
                    auth.logout(request)
                    request.session['warning'] = unicode(_("You have been logged out"))
                    return
            except Exception as e:
                # It should not reach this point
                auth.logout(request)
                del request.session['last_touch']
                request.session['warning'] = unicode(_("You have been logged out"))

        except KeyError:
            pass

         request.session['last_touch'] = str(datetime.now())
like image 26
brunofitas Avatar answered Sep 17 '22 15:09

brunofitas