aithine: (Default)
Aithine ([personal profile] aithine) wrote in [community profile] django_dev2010-05-19 02:57 pm

Full page URL on page template

Say you need the full URL of the current page you're displaying that you need to use on the template of that page. (For example, to add links to Facebook, Delicious, etc., and you don't want to have additional JavaScript that you didn't write on your pages. Shush, yes, I'm a control freak. :P) Also say that you don't want to use the sites framework to hack it together. The permalink decorator won't work, either, because that gives you the relative link.

Thankfully, there's a new request object that's been added to 1.0 that will do it.

To use it, include the bolded bit in your views.py file, as part of each view where you want to have the variable available:

return render_to_response('dir/template.html', {'object': obj, 'link': request.build_absolute_uri()}, RequestContext(request))

Then you can use the link variable on the template where you need the URL:

<a href="http://www.facebook.com/sharer.php?u={{ link|urlencode }}&t={{ title|urlencode }}">
foxfirefey: A picture of GIR. (gir)

[personal profile] foxfirefey 2010-05-19 10:43 pm (UTC)(link)
Ooooo.