I found myself putting `{%load ... %}` in every template that I was writing, so DRY .. I created an app called 'globaltags' and in its `__init__.py`, I just pre-load the tags that I use frequently.
The [pyif](http://www.djangosnippets.org/snippets/130/) and [expr](http://www.djangosnippets.org/snippets/9/) tags are excellent tags, and I highly recommend them for getting the most out of django's template language.
The [dbinfo](http://www.djangosnippets.org/snippets/159/) snippet is something that I came up with to easily output SQL debugging information.
MintCache is a caching engine for django that allows you to get by with stale data while you freshen your breath, so to speak.
The purpose of this caching scheme is to avoid the dog-pile effect. Dog-piling is what normally happens when your data for the cache takes more time to generate than your server is answering requests per second. In other words if your data takes 5 seconds to generate and you are serving 10 requests per second, then when the data expires the normal cache schemes will spawn 50 attempts a regenerating the data before the first request completes. The increased load from the 49 redundant processes may further increase the time it takes to generate the data. If this happens then you are well on your way into a death spiral
MintCache works to prevent this scenario by using memcached to to keep track of not just an expiration date, but also a stale date The first client to request data past the stale date is asked to refresh the data, while subsequent requests are given the stale but not-yet-expired data as if it were fresh, with the undertanding that it will get refreshed in a 'reasonable' amount of time by that initia request
I don't think django has a mechanism for registering alternative cache engines, or if it does I jumped past it somehow. Here's an excerpt from my cache.py where I'v just added it alongside the existing code. You'll have to hook it in yourself for the time being. ;-)
More discussion [here](http://www.hackermojo.com/mt-static/archives/2007/03/django-mint-cache.html).
This is a simple filter I use to display a list of links from a blog entry off in the sidebar ([example](http://www2.jeffcroft.com/blog/2007/feb/25/two-new-django-sites-both-source-available/)).
Requires beautifulsoup. Originally by [Nathan Borror](http://playgroundblues.com), tweaked slightly by me.
Finds all ``<code></code>`` blocks in a text block and replaces it with pygments-highlighted html semantics. It tries to guess the format of the input, and it falls back to Python highlighting if it can't decide. This is useful for highlighting code snippets on a blog, for instance.
While checking up on some cronjobs at [YouTellMe](http://www.youtellme.nl/) we had some problems with large cronjobs that took way too much memory. Since Django normally loads all objects into it's memory when iterating over a queryset (even with .iterator, although in that case it's not Django holding it in it's memory, but your database client) I needed a solution that chunks the querysets so they're only keeping a small subset in memory.
Example on how to use it:
`my_queryset = queryset_iterator(MyItem.objects.all())
for item in my_queryset:
item.do_something()`
[More info on my blog](http://www.mellowmorning.com/2010/03/03/django-query-set-iterator-for-really-large-querysets/)
An email address obfuscation template filter based on the ROT13 Encryption function in Textmate's HTML bundle.
The filter should be applied to a string representing an email address. You can optionally pass the filter an argument that will be used as the email link text (otherwise it will simply use the email address itself).
Example usage:
{{ email_address|obfuscate:"Contact me!" }}
or
{{ email_address|obfuscate }}
Of course, you can also use this on hardcoded email addresses, like this:
{{ "[email protected]"|obfuscate }}
Special thanks to the author of snippet 769 who provided most of the code for this snippet.
Major differences:
1.Simpler/better handling of "extends" block tag
2.Searches If/Else blocks
3.Less code
4.Allow list of templates to be passed which is closer to the behavior of render_to_response
A FileField Widget that displays an image instead of a file path if the current file is an image. Could also be used with sorl.thumbnail to generate thumbnail images.
**Example**
class FileUploadForm(forms.ModelForm):
upload = forms.FileField(widget=AdminThumbnailWidget)
class Meta:
model = FileUpload
class FileUploadAdmin(admin.ModelAdmin):
form = FileUploadForm
admin.site.register(FileUpload, FileUploadAdmin)
The views.py used by [wikinear.com](http://wikinear.com/) - see [http://simonwillison.net/2008/Mar/22/wikinear/](http://simonwillison.net/2008/Mar/22/wikinear/)
For a more comprehensive API wrapper for Fire Eagle, take a look at [fireeagle_api.py](http://github.com/SteveMarshall/fire-eagle-python-binding/tree/master/fireeagle_api.py)
Place this snippet at the bottom of your settings.py file, and if a local_settings.py file is present in the directory, all settings in that file will override those in settings.py
The Django docs show us how to give models a custom manager. Unfortunately, filter methods defined this way cannot be chained to each other or to standard queryset filters. Try it:
class NewsManager(models.Manager):
def live(self):
return self.filter(state='published')
def interesting(self):
return self.filter(interesting=True)
>>> NewsManager().live().interesting()
AttributeError: '_QuerySet' object has no attribute 'interesting'
So, instead of adding our new filters to the custom manager, we add them to a custom queryset. But we still want to be able to access them as methods of the manager. We could add stub methods on the manager for each new filter, calling the corresponding method on the queryset - but that would be a blatant DRY violation. A custom `__getattr__` method on the manager takes care of that problem.
And now we can do:
>>> NewsManager().live().interesting()
[<NewsItem: ...>]
A URL field specifically for images, which can validate details about the filesize, dimensions and format of an image at a given URL, without having to read the entire image into memory.
Requires [Python Imaging Library](http://www.pythonware.com/library/pil/).
*4th October, 2008* - updated for 1.0 compatibility.
QLeftOuterJoin object allows you to create 'LEFT OUTER JOIN' sql query. It is very usefull if you have to define ForeignKey to 'null=True' (select_related will not work with null=True).
You are allowed to use QLeftOuterJoin like Q object.
Example:
`QLeftOuterJoin('thread_last_post', Post, 'pk', Thread, 'last_post')`
It will generates SQL like:
LEFT OUTER JOIN appname_post AS thread_last_post ON thread_last_post.id = appname_thread.last_post_id
Table could be model or string.
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.