# -*- coding: utf-8 -*-## Copyright (c) 2009 Arthur Furlan <arthur.furlan@gmail.com>## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## On Debian systems, you can find the full text of the license in# /usr/share/common-licenses/GPL-2importredefget_referer_view(request,default=None):''' Return the referer view of the current request Example: def some_view(request): ... referer_view = get_referer_view(request) return HttpResponseRedirect(referer_view, '/accounts/login/') '''# if the user typed the url directly in the browser's address barreferer=request.META.get('HTTP_REFERER')ifnotreferer:returndefault# remove the protocol and split the url at the slashesreferer=re.sub('^https?:\/\/','',referer).split('/')ifreferer[0]!=request.META.get('SERVER_NAME'):returndefault# add the slash at the relative path's view and finishedreferer=u'/'+u'/'.join(referer[1:])returnreferer