How to get URL of current page, including parameters, in a template?
Solution 1:
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:
- Custom Context Processors
- HTTPRequest's get_full_path() method.
Solution 2:
Use Django's build in context processor to get the request in template context. In settings add request
processor to TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS = (
# Put your context processors here
'django.core.context_processors.request',
)
And in template use:
{{ request.get_full_path }}
This way you do not need to write any new code by yourself.
Solution 3:
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}}