I was wondering how to get the current URL within a template.
Say my current URL is:
.../user/profile/
How do I return this to the template?
Use handy request. build_absolute_uri() method on request, pass it the relative url and it'll give you full one. By default, the absolute URL for request. get_full_path() is returned, but you can pass it a relative URL as the first argument to convert it to an absolute URL.
The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py . Listing 2-16 shows how to name a project's home page, as well as how to reference this url from a view method or template.
8. What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.
Django 1.9 and above:
## template {{ request.path }} # -without GET parameters {{ request.get_full_path }} # - with GET parameters
Old:
## settings.py TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.request', ) ## views.py from django.template import * def home(request): return render_to_response('home.html', {}, context_instance=RequestContext(request)) ## template {{ request.path }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With