python - In django : how to renew expiry date for current session? -


I have a user logged in. How can I extend / renew the expiration date of the request received from the request? thank you in advanced!

Here are some middleware that increases the authenticated user's session, it is essentially theirs permanently Expand the session by logging in to more than 60 days if their session is less than 30 days away.

custom_middleware.py: Import time from Django.utils import timetable EXTENDED_SESSION_DAYS = 60 EXPIRE_THRESHOLD = 30 square ExtendUser session (object): "" "Authenticated user sessions So that they do not have to log back in each. 2 weeks (the default of Django is set by the setting 'SESSION_COOKIE_AGE`). " "Def process_request (self, request): # requestuth.user.is_authenticated (#): now = timezone. # If only the current expiry_date is less than 30 days, then only expand the session if request.session.get_expiry_date () & lt ; Now + TimeDelta (day = EXPIRE_THRESHOLD): request.session.set_expiry (now + timedelata (day = EXTENDED_SESSION_DAYS))

You will need to add this custom middleware after that

project / settings.py:

  MIDDLEWARE_CLA SSES = ['Django.contrib.sessions.middleware.SessionMiddleware', 'project.custom_middleware.ExtendUserSession', ...]  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -