Django: Capture blocks of template data and apply filters

The ability for Django to spit data out to the template so easily using tags and filters is really one of its strengths.

However, if you need to alter the output of some tags and they don't support an option to store the data as a context variable, things might get a little hairy.

Luckily, there is an easy way to apply filters to that data and its even built into the framework.

Use the "filter" tag to apply one or more filters to the data.

<a href="/do/something/here?url={% filter urlencode %}
{% url video-feed '' %}pubid;{{ article.source_publication.pubid }};id;{{ article.id }}
{% endfilter %}&ampid=42" target="_blank">hello</a>

This will apply the urlencode filter to the output URL only, and the rest of the URL will remain untouched.

You can also chain the filters as you do normally, so there isn't a need to nest the filter tags.

{% filter force_escape|lower %}

If you're looking for a way to capture the actual template output to a variable, then use the captureas template tag.

What information does Google have about me?

After signing into my Google account on my Android phone, I quickly realised how much information Google had stored on my activities online and how much it actually sync'd into my phone.

For example; Google Talk, Gmail, Picasa albums, calendar, Google Docs, AdSense, Analytics, blogspot, YouTube, Google Map locations, Google Buzz, Google Reader, Feed Burner and whatever else linked to my Google account.

Feeling a little creeped out, I stumbled upon Google Dashboard, which lets you see all the data they have on you and your digital life.

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog