Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current URL within a Django template?

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?

like image 491
dotty Avatar asked May 21 '10 13:05

dotty


People also ask

How can I get full URL in Django?

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.

How do I reference a URL in Django?

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.

What does {{ name }} this mean in Django templates?

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.


1 Answers

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 }} 
like image 125
httpete Avatar answered Oct 11 '22 11:10

httpete