How can I set session never expire in django

user2307087

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

brunofitas

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())

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make a php session to never expire (in the conf file, no code)

From Dev

How can I make a php session to never expire (in the conf file, no code)

From Dev

How to expire session in php?

From Dev

How to expire session in php?

From Java

How do I expire a PHP session after 30 minutes?

From Dev

Set Google Drive Push Notification to Never Expire

From Dev

Set Google Drive Push Notification to Never Expire

From Dev

How not to expire user session cakephp

From Dev

How can I set a session variable in a servlet and get it in a JSP?

From Dev

How can I set a single proxy for a requests session object?

From Dev

How I can set a session in codeigniter 3 database?

From Dev

how can I set a callback for the user session timeout

From Dev

How can I set a session variable in a servlet and get it in a JSP?

From Dev

how can I set session id and verify on the next page in php

From Dev

How can i find django user from cached backend session

From Dev

How can i find django user from cached backend session

From Dev

Why is total_items larger than curr_items in Memcached when I have plenty of memory and items are set to never expire?

From Dev

How can I automatically expire messages on an Azure Service Bus subscription?

From Dev

How can I make this Concurrent Dictionary expire with a timer?

From Dev

Can a Java Session expire if there is an object reference to it?

From Dev

can not set expire time with `SET` in redis

From Dev

How do I force expire a session if a cart_id no longer exists?

From Dev

How do I force expire a session if a cart_id no longer exists?

From Dev

How do I set up permissions in django-rest-framework so that the session user can only list objects which have a foreign key to that user?

From Dev

How do I set up permissions in django-rest-framework so that the session user can only list objects which have a foreign key to that user?

From Dev

how can i set session in setup when i test phoenix action which need user_id in session?

From Java

How can i set custom pagination in Django Rest Framework?

From Java

How can i set pagination dynamically in Django Rest Framework?

From Dev

How can I set a DateField format in django from the model?

Related Related

  1. 1

    How can I make a php session to never expire (in the conf file, no code)

  2. 2

    How can I make a php session to never expire (in the conf file, no code)

  3. 3

    How to expire session in php?

  4. 4

    How to expire session in php?

  5. 5

    How do I expire a PHP session after 30 minutes?

  6. 6

    Set Google Drive Push Notification to Never Expire

  7. 7

    Set Google Drive Push Notification to Never Expire

  8. 8

    How not to expire user session cakephp

  9. 9

    How can I set a session variable in a servlet and get it in a JSP?

  10. 10

    How can I set a single proxy for a requests session object?

  11. 11

    How I can set a session in codeigniter 3 database?

  12. 12

    how can I set a callback for the user session timeout

  13. 13

    How can I set a session variable in a servlet and get it in a JSP?

  14. 14

    how can I set session id and verify on the next page in php

  15. 15

    How can i find django user from cached backend session

  16. 16

    How can i find django user from cached backend session

  17. 17

    Why is total_items larger than curr_items in Memcached when I have plenty of memory and items are set to never expire?

  18. 18

    How can I automatically expire messages on an Azure Service Bus subscription?

  19. 19

    How can I make this Concurrent Dictionary expire with a timer?

  20. 20

    Can a Java Session expire if there is an object reference to it?

  21. 21

    can not set expire time with `SET` in redis

  22. 22

    How do I force expire a session if a cart_id no longer exists?

  23. 23

    How do I force expire a session if a cart_id no longer exists?

  24. 24

    How do I set up permissions in django-rest-framework so that the session user can only list objects which have a foreign key to that user?

  25. 25

    How do I set up permissions in django-rest-framework so that the session user can only list objects which have a foreign key to that user?

  26. 26

    how can i set session in setup when i test phoenix action which need user_id in session?

  27. 27

    How can i set custom pagination in Django Rest Framework?

  28. 28

    How can i set pagination dynamically in Django Rest Framework?

  29. 29

    How can I set a DateField format in django from the model?

HotTag

Archive