djangodjango-templatesdjango-urlsdjango-request

How to get URL of current page, including parameters, in a template?


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


Solution

  • 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: