Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get full URL(include domain) within Django template

I found the tag {% url path.to.view %} can only return the path of URL, how can I get the full URL with domain name?

Actually, what I want to do is, adding a link which point to another view of my site. But {% url path.to.view %} can only get the path of my view. In result, the link cannot point to what I want.

My solution is using HttpRequest.get_host() in first view to get domain and pass it to template by Context. Then, in template(html file), splice to the full url address. Like this:<a href="http://{{ domain }}{% url path.to.view %}?param={{param}}">Foo</a>.

In short, I just want to get domain. Sorry for my unclear description! @Hans's answer is great, thank you!

like image 639
iStar Avatar asked Sep 28 '12 08:09

iStar


1 Answers

That's in the docs here:

Use the method build_absolute_uri() on the request object.

like image 74
Hans Then Avatar answered Nov 10 '22 03:11

Hans Then