Wednesday, April 6, 2011

Get the current URL within a django template

Hay, i was wondering how to get the current URL within a template.

Say my URL was

/user/profile/

How do i return this to the template?

Thanks

From stackoverflow
  • request.get_full_path

    dotty : Can i get it without passing the 'request' variable to the template?
    Brant : I believe request is always passed to the template... if not, render_to_response('template.html',{},context_instance=RequestContext(request)) .. at that point, request is definitely passed in.
    dotty : Bascially i'm making a 'logout' function which redirects back to the page they were on when a 'logout' button is clicked. I was going to append the current URL to the query string.
    Brant : Just use request.path then
    dotty : I decided to use request.META['HTTP_REFERER'] to redirect them.
    Jack M. : Be warned that HTTP_REFERRER isn't always reliable. Many of the "Internet Security Suites" will remove that variable. It is probably worth it to ensure that your site still works even when there isn't a referrer, using something like `request.META.get('HTTP_REFERRER', '/')` or similar.

0 comments:

Post a Comment