Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current URL of a page in a Django template?

Tags:

python

django

I know there is another question with virtually the same title as mine but the solution in that one didn't work for me. My url is like this:

http://domain.com/videos/dvd/1/

If I use either {{baseurl}} or {{ request.get_full_path }} I get just this part:

http://domain.com/videos/

How can I get the entire url? I need to be able to do this from the template level.

EDIT

P.S. it should disregard any parameters that may be in the url.

like image 365
TJ Desantes Avatar asked Jul 11 '11 15:07

TJ Desantes


People also ask

How can I get current URL in Django?

This is an old question but it can be summed up as easily as this if you're using django-registration. In your Log In and Log Out link (lets say in your page header) add the next parameter to the link which will go to login or logout. Your link should look like this.

What does form {% URL %} do?

{% url 'contact-form' %} is a way to add a link to another one of your pages in the template. url tells the template to look in the URLs.py file. The thing in the quotes to the right, in this case contact-form , tells the template to look for something with name=contact-form .

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

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

You could get it in your view and pass it along into your template context so that it is available to you there.

https://docs.djangoproject.com/en/1.3/ref/request-response/#django.http.HttpRequest.build_absolute_uri

full_url = request.build_absolute_uri(None)

# pass full_url into the template context.
like image 162
Ken Cochrane Avatar answered Oct 05 '22 01:10

Ken Cochrane