views:

49

answers:

2

Is there a way to get the current page URL and all its parameters in a Django template?

For example, a templatetag that would print full URL like /foo/bar?param=1&baz=2

+1  A: 

In a file context_processors.py (or the like):

def myurl( request ):
  return { 'myurlx': request.get_full_path() }

In settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
  ...
  wherever_it_is.context_processors.myurl,
  ...

In your template.html:

myurl={{myurlx}}
eruciform
+2  A: 

Write a custom context processor. e.g.

def get_current_path(request):
    return {
       'current_path': request.get_full_path()
     }

add a path to that function in your TEMPLATE_CONTEXT_PROCESSORS settings variable, and use it in your template like so:

{{ current_path }}

See:

sdolan