Skip to content

Instantly share code, notes, and snippets.

@tianweidut
Created April 18, 2013 07:58
Show Gist options
  • Save tianweidut/5411009 to your computer and use it in GitHub Desktop.
Save tianweidut/5411009 to your computer and use it in GitHub Desktop.
How to jump last visited url in Django? (Django cannot provide build-in method and the request.META.REFERER can't work in all scenarios), so write a custom middleware is necessary.
#it is in the backend package
class PreviousURLMiddleware(object):
def process_response(self, request, response):
if response.status_code == 200:
try:
request.session["previous_url"] = request.session["current_visiting"]
except:
pass
request.session["current_visiting"] = request.get_full_path()
return response
INSTALLED_APPS += ('backend')
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'backend.custom_middlewares.PreviousURLMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment