April 24, 2010 14:36 / 4 comments / django python 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)

Commenting has been closed, but please feel free to contact me