A handful of snippets

Below I've grouped together some snippets I've made use of recently.

Problem: you want to limit posts to a view

This can be accomplished with a view decorator that stores hits by IP in memcached, incrementing the cached value and returning 403's when the cached value exceeds a certain threshold for a given IP.

http://www.charlesleifer.com/projects/gists/simple-rate-limiting-decorator/

Problem: you want to get the latest model instances into a template easily

Writing templatetags is obnoxious. Say you have a small blurb on all your pages that shows the latest 5 comments posted to the site -- using this filter, you could write the following:

{% for comment in "comments.comment"|latest:5 %}...display comment here...{% endfor %}

http://www.charlesleifer.com/projects/gists/latest-model-instance-filter/

Problem: you have a template filter you'd like to cache

Say you'd like to cache one of your template filters. This decorator acts sort of like memoize, caching a result set based on the arguments passed in (which are used as the cache key).

http://www.charlesleifer.com/projects/gists/caching-template-filters/

Comments (4)

  1. Nice, thanks for sharing. I am not clear why you would like to cache a template filter though?

  2. You should share your useful snippets on djangosnippets :)

  3. +1 for djangosnippets It would be easier to find them

  4. Yo, I was having trouble registering with djangosnippets but it turns out the message was just getting sent to my spam folder. Will post there -- thanks for the tip.

Commenting has been disabled for this entry

If you'd like to discuss an aspect of this post, feel free to contact me via email.