Django: get current url path from actual current page

13,779

Instead of using request.get_full_path(), which will give you path of search view, use referrer from the HTTP headers.

You can get that with request.META['HTTP_REFERER']

Share:
13,779
vt2424253
Author by

vt2424253

I am a novice programmer. Presently improving my web development skills in python, Django, HTML, Javascript, CSS, and MySQL. If you know how to code these things, I hope you can share. I love to learn from you.

Updated on June 05, 2022

Comments

  • vt2424253
    vt2424253 about 2 years

    I have a situation which I hope you can help me. I have read a few post and answers about getting current url path on SO current_url and url TEMPLATE_CONTEXT_PROCESSORS (which are most relevant). But it doesn't seem to fit what I am trying to do. I have a view:

    def fish_search(request):
        args = {}
        #irrelevant code here
        args['fishes'] = fishes
        args['current_path'] = request.get_full_path()
        return render_to_response('ajax_search.html', args)
    

    In my ajax_search.html:

    <a id="search-results" href="{{ current_path }}"></a>
    

    And base.html:

    div id="search-results" ></div>
    

    Javascript dumps the search results to base.html. And base.html is extended in fishMarket.html, fishDictionary.html, fishRumour.html, etc. So, sadly, the paths that show up are all "/search/"

    I want the path to be /fishMarket/ if I am searching from fishMarket.html, /fishDictionary/ should show up if I am searching from fishDictionary.html, and likewise, /fishRumour/ if I am searching from fishRumour.html. Have anyone come across this type of situation? How did you solve this problem? I'm relatively new to django, so please dumb down the solution.

    I really appreciate your help. many thanks!

  • vt2424253
    vt2424253 about 10 years
    Thanks so much for the solution. But I got the following errors when replacing request.get_full_path() with request.META['HTTP_REFERRER'] and the error is KeyError: 'HTTP_REFERRER' KeyError: 'HTTP_REFERRER'
  • patsweet
    patsweet about 10 years
  • Rohan
    Rohan about 10 years
    @vt2424253, sorry the HTTP_REFERRER is mispelled, it should be HTTP_REFERER - with single R
  • vt2424253
    vt2424253 about 10 years
    Thanks! I was doing some search and that's what I discovered, too. btw, if you think this question could be useful for others on SO, please up vote the question. I really appreciate your help.