- May 22, 2007
- middleware session user
- 1 (after 1 ratings)
This middleware remembers the last URLs that the user visited on your Django-site and saves them into the
The fields are
currently_visiting for the URL that is opened by the user and
last_visited which is the URL before. Most of the time, you'll need only
currently_visiting is just an implementation detail.
This snippet was created because I needed to redirect the user to the page he's been watching before clicking on the "translate" link. One other alternative would be adding the URL the user was visiting as a GET field to the "translate" link, but I hoped to find a possibility to avoid GET.
This snippet works quite well as a proof of concept, the only known wart is when the user uses tabs or multible windows on the site, things get messed up. This cannot be solved, it's a restriction imposed by the design of HTTP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/env python # -*- coding: UTF-8 -*- class LastVisitedMiddleware(object): """This middleware sets the last visited url as session field""" def process_request(self, request): """Intercept the request and add the current path to it""" request_path = request.get_full_path() try: request.session['last_visited'] = request.session['currently_visiting'] except KeyError: # silence the exception - this is the users first request pass request.session['currently_visiting'] = request_path
More like this
- Effective content caching for mass-load site using redirect feature by nnseva 3 years, 8 months ago
- Datetime widget by pigletto 7 years, 7 months ago
- LocaleMiddleware without browser language discovery by ivellios 1 month ago
- Url filter middleware by limodou 8 years, 1 month ago
- YUI Loader as Django middleware by akaihola 6 years, 11 months ago