Login

Top-rated snippets

Snippet List

@url decorator improvements

A slight modification (and, I think, improvement) of the URL decorator found in [snippet 395](http://www.djangosnippets.org/snippets/395/). What's different between this snippet and 395? 1. We use `django.conf.urls.defaults.url()` when adding patterns 2. We support arbitrary arguments to the `url()` method (like `name="foo"`) 3. We _do not_ support multiple url patterns (this didn't seem useful to me, but if it is I can add it back.)

  • urls
  • url
  • decorator
  • decorators
  • urlpatterns
Read More

S/MIME Encrypted E-mail

Requires the M2Crypto module. See [http://sandbox.rulemaker.net/ngps/m2/howto.smime.html](http://sandbox.rulemaker.net/ngps/m2/howto.smime.html) for more information on using M2Crypto to create S/MIME email. This could also be adapted to allow signing, or sign+encrypt, but currently only encrypts. Use just like `EmailMessage`, except takes an extra parameter `cert`, which is the path to the recipient's public X.509 certificate.

  • email
  • emailmessage
  • encryption
Read More

Hidden Date Display Widget for Admin

This is a custom widget for displaying a view only date field in the django admin. I used it to get around this ticket: [http://code.djangoproject.com/ticket/342](http://code.djangoproject.com/ticket/342)

  • admin
  • view
  • date
  • widgets
  • field
  • only
Read More

Prevent Django newcomments spam with Akismet (reloaded)

This is a rewrite of [snippet #1006](http://www.djangosnippets.org/snippets/1006/) to use the moderation features available in Django's comments framework. This is more customizable than the signals approach and works well if other moderation features are being used. If you want to make comments that are flagged as spam become hidden instead of deleted, change the allow() method to moderate(). [See the blog post here](http://sciyoshi.com/blog/2009/jul/17/prevent-django-newcomments-spam-akismet-reloaded/)

  • akismet
  • spam
  • comments
  • antispam
  • newcomments
Read More

Custom management command to list recent admin actions

On a busy site it can be nice to have a summary of admin activity. Running this command (I call it "adminlog") generates output like this: 2009-07-10 18:06:19: pbx changed flat page: "/yay/ -- Let's All Say Yay" By default it shows the last five actions; pass it a numerical arg to show more or fewer. Run this as a cron job and you can follow a site's admin-side activity without even logging in!

  • admin
  • commands
Read More
Author: pbx
  • 1
  • 3

slug filename

A one-liner that I use all the time: Set `upload_to` to something based on the slug and the filename's extension. Just add this function to the top of your models and use it like this: image = models.FileField(upload_to=slug_filename('people')) and the `upload_to` path will end up like this for eg `myfile.jpg`: people/this-is-the-slug.jpg Enjoy!

  • upload_to
Read More

TRAC-Ticket on exception

This is a small approach to have a middleware which automatically creates a ticket in an existing Trac environment. **Note:** you must have the [XML-RPC-Plugin](http://trac-hacks.org/wiki/XmlRpcPlugin) installed. Extend the attrs-dict to your needs. For example: in my case I have the SensitiveTicket-Plugin installed - automatically created tickets are marked as sensitive and are not visible to the public.

  • middleware
  • exception
  • trac
  • ticket
Read More

improved sortby template tag

Variation on dictsort using attribute access. Nested attributes can be used, like, "obj.attr.attr_attr" Example usage: {% for entry in entries|sortby:'category.title' %} Based on [1609](http://www.djangosnippets.org/snippets/1609/)

  • template
  • tag
  • dictsort
Read More

sortby template tag

This is a variation on dictsort that assumes objects with attributes, not dictionaries. Example usage: {% for book in author.book_set.all|sortby:'title' %}

  • template
  • dictsort
Read More

Ensure ugettext is available absolutely everywhere

Put this into the __init.py__ file in the root of your project (the same directory level as urls.py and settings.py) and this installs _() as a global reference into the current running python VM, and now it’s as universally available as int(), map(), or str(). This is, of course, controversial. Modifying the python global namespace to add a function can be considered maintenance-hostile. But the gettext feature is so universal– at least to me– that __init__.py is where it belongs.

  • internationalization
  • gettext
Read More

Ensure submitted slugs do not conflict with existing resolvable URLs

This code overrides the existing RegistrationForm in django-registration and adds a new validation step. In this step, the username (my example slug) is compared against all the existing URLs that the application currently resolves and, if it *does* successfully resolve, throws a validation exception. This indicates that the username chosen would be overriden (or, if you wrote your urls.py file badly, would override) an existing URL already recognized by your application. A much longer explanation can be found at [Dynamic names as first-level URL path objects in Django](http://www.elfsternberg.com/2009/06/26/dynamic-names-as-first-level-url-path-objects-in-django/).

  • registration
  • slug
  • reverse
  • resolve
Read More

invalidation of cache-template-tag cache

this function invalidates a template-fragment cache bit. say you have: {% load cache %} {% cache 600 user_cache user.id %} something expensive here {% endcache %} maybe you want to force an update. With this function you can, just call: invalidate_template_cache("user_cache", user.id)

  • template
  • cache
  • invalidate
Read More

3110 snippets posted so far.