3

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!

Community
  • 1
  • 1
vt2424253
  • 1,387
  • 4
  • 25
  • 39

1 Answers1

8

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']

Rohan
  • 52,392
  • 12
  • 90
  • 87
  • 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' – vt2424253 Apr 12 '14 at 05:15
  • 2
    `REFERER` is misspelled. https://docs.djangoproject.com/en/1.6/ref/request-response/#django.http.HttpRequest.META – patsweet Apr 12 '14 at 05:39
  • @vt2424253, sorry the HTTP_REFERRER is mispelled, it should be HTTP_REFERER - with single R – Rohan Apr 12 '14 at 05:40
  • 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. – vt2424253 Apr 13 '14 at 01:10