Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1026827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:08:34+00:00 2026-05-16T12:08:34+00:00

I want to be able to create a back link to the referring URL,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:08:35+00:00Added an answer on May 16, 2026 at 12:08 pm

    Use django.core.urlresolvers.resolve to figure out if it’s an internal Django URL or not. If it is not, it will raise django.core.urlresolvers.Resolver404, otherwise it will return a match object you can introspect if necessary. You can feed the REFERER environment variable to this as necessary, and replace the URL with a default URL if resolve raises Resolver404.

    edit: Actually, no, resolve apparently only works with path components of URLs. So you’d have to deconstruct the REFERER header using urlparse to figure out if it’s the right domain, and if it is, use resolve on the path component of the parsed URL to figure out if the URL is part of your Django site or not.

    # code not tested, YMMV, etc. 
    
    from urlparse import urlparse
    
    from django.core import urlresolvers
    
    HOME = 'http://my.site.com/'
    
    def generate_back_url(url, default_url=HOME):
        parsed = urlparse(url)
        if parsed.netloc == 'my.site.com':
            try:
                urlresolvers.resolve(parsed.path)
            except urlresolvers.Resolver404:
                back_url = default_url
            else:
                back_url = url
        else:
            back_url = default_url
        return back_url
    
    # ...
    
    def my_view(request):
        # ...
        # truth be told I'm not sure whether the .get() is necessary, sorry.
        back_url = generate_back_url(request.META.get('REFERER', HOME))
        # ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to create a Pandas DataFrame with MultiIndexes for the
I want to be able to create links looking like buttons with dynamic text
I want to be able to create modal dialogs, with, for example close: function()
I want to be able to create a temporary text File in Java to
I want to be able to create the XSD file for my typed dataset
I want to be able to create a KPI List on my MOSS 2007
I want to be able to create a file from the Content Provider, however
hey i want to be able to create multiple shapes and store them perhaps
We want people to be able to create a thumbnail version of an uploaded
I want to be able to dynamically create ViewControllers based on a JSON file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.