Django: get URL of current page, including parameters, in a template

Matt Hampel picture Matt Hampel · Jul 14, 2010 · Viewed 77.5k times · Source

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 a full URL like /foo/bar?param=1&baz=2

Answer

Sam Dolan picture Sam Dolan · Jul 14, 2010

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 }}

If you want to have the full request object in every request, you can use the built-in django.core.context_processors.request context processor, and then use {{ request.get_full_path }} in your template.

See: