I want to be able to create a back link to the referring URL, if the referrer was a view, and if it was not a view, a back link to a default page (Don’t ask… it’s kind of a weird requirement).
Basically, if the user came to the page in question from another view on the same django site, the back link should be a return to that view.
If the user came in from an external site, the back link should go to a default view.
Javascript is not an ideal solution, although I’m willing to consider it if there’s no other way.
Use
django.core.urlresolvers.resolveto figure out if it’s an internal Django URL or not. If it is not, it will raisedjango.core.urlresolvers.Resolver404, otherwise it will return a match object you can introspect if necessary. You can feed theREFERERenvironment variable to this as necessary, and replace the URL with a default URL ifresolveraises Resolver404.edit: Actually, no,
resolveapparently only works with path components of URLs. So you’d have to deconstruct theREFERERheader using urlparse to figure out if it’s the right domain, and if it is, useresolveon the path component of the parsed URL to figure out if the URL is part of your Django site or not.