Monday, November 29, 2010

Re: absolute url in template and url name

As a followup, I had to do something similar and ended up using this:

# Gets the http://domain.com without the trailing /
base_url = request.build_absolute_uri('/')[:-1]

And in the template I can do this:

<a href="{{ base_url }}{% url some_view_name %}">...</a>

The benefits:
-You only have to create the base_url in the view, the rest you can do
in the template itself.
-The URLs are absolute & contain the domain (ie. http://www.domain.com/page/1,
https://www.domain.com:8008/view2, etc).
-Includes http & https.
-Simple.

This the method I'm using from now on.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment