I have a user logged in. How can i extend/renew expiry date of session received from the request ? Thanks in advance!
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Django __str__ returned non-string (type NoneType)
- Evil ctypes hack in python
Here's some middleware that extends an authenticated user's session. It essentially keeps them permanently logged in by extending their session another 60 days if their session
expiry_date
is less than 30 days away.custom_middleware.py:
You will then need to add this custom middleware after Django's SessionMiddleware, so your settings file should look like:
project/settings.py:
It's not necessary to make a custom middleware for this.
Setting
SESSION_SAVE_EVERY_REQUEST = True
will cause Django's existingSessionMiddleware
to do exactly what you want.It has this code:
setting SESSION_COOKIE_AGE is designed for that purpose I believe. After login cookie is set automatically for this period.
You can also save session cookie on every request by using SESSION_SAVE_EVERY_REQUEST setting.