May. 19th, 2010

aithine: (Default)
[personal profile] aithine
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 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="{{ link|urlencode }}&t={{ title|urlencode }}">
foxfirefey: Fox stealing an egg. (Default)
[personal profile] foxfirefey

This is a filter that turned out to be not quite as useful as I thought because some of the phrases our graphic designer wanted more than one word bolded, but I could totally change around the argument of this filter to take in a number of words to enclose, instead of making the class the optional argument. But, I thought it made a good basic example, so I figured I'd post it. It's useful if you want to visually emphasize the first word of a string, since there's no CSS pseudoselector for that.

On to the example code! )


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

April 2014

131415161718 19

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags