aithine: (Default)
[personal profile] aithine posting in [community profile] django_dev
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 }}">

(no subject)

Date: 2010-05-19 10:43 pm (UTC)
foxfirefey: A picture of GIR. (gir)
From: [personal profile] foxfirefey
Ooooo.

Profile

django_dev: The Django logo. (Default)
Developing with Django

April 2014

S M T W T F S
  12345
6789101112
131415161718 19
20212223242526
27282930   

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags