I would like to know if there is a better solution for this.
The website I’m building has some forms which need information about the page the user started his ‘journy’. Right now I’m saving HTTP_REFERER to the session and use it later on.
A disadvantage is the need for set_expiry(0) which clears the session on browser close. I like the behavior that users don’t need to login every time they close the browser.
I wrote a little middleware class that looks like this:
class RefererMiddleware(object):
def process_response(self, request, response):
try:
if not request.session.get('http_landingpage'):
request.session['http_landingpage'] = request.META.get('HTTP_REFERER')
request.session.set_expiry(0)
except Exception:
pass
return response
Any suggestions for improvement? Other solutions?
What about setting a cookie, which will expire when the browser is closed. You can do it in the middleware and in the end the session will be left intact.