Login

Most bookmarked snippets

Snippet List

django_stateful

this snippet provides a class that can be subclassed for creating views that retain state between requests, you can read more here http://code.google.com/p/django-stateful/ your comments are welcome!

  • django
  • stateful
  • seaside
  • continuations
Read More

Chunks template filter

A simple filter which divides an iterable (list, tupe, string, etc) in chunks, which can then be iterated over separately. A sample of the filter usage is given: a gallery template in which I needed to display images in a table, three images per row, one row for images followed by one row for their descriptions.

  • template
  • filter
  • chunks
  • for
  • iterable
Read More

StrictAuthentication - Auto log-out inactive users

This dead-simple piece of middleware adds a terrific security feature to django authentication. Currently, users who's accounts are de-activated still may have a cookie and a login session. This middleware destroys that session on their next request. Simply add this class into a middleware.py and add it to your settings.

  • middleware
  • authentication
  • security
  • sessions
Read More

EncryptField

I was trying to create a custom field to use the mysql encrypt() function on some data I wanted to store in the DB. initcrash on IRC pointed me to [this code](https://tracpub.yaco.es/cmsutils/browser/trunk/db/fields.py?rev=66) which I butchered as best as my little brain could. Amazingly enough I got it working (thanks to a couple answers from initcrash). If anyone can clean this up that would be great. I'm also trying to figure out how I can a) create password reset link in the admin interface for this field without displaying the field or b) decrypt it so that the password field is pre-populated with the decrypted password. Otherwise it is submitting the encrypted string as a new password. Anyways, I'm only a week or two into Django with no python experience so any suggestions are very welcome. Hope this helps someone!

  • mysql
  • crypt
  • custom-field
  • encrypt()
Read More

CheckedField

Here's a snippet to pair an arbitrary form Field type (the "target field") with a checkbox. If the checkbox is not selected, the cleaned_data value for the field will be None. If the checkbox is selected, it will return the target field's cleaned value.

  • checkbox
  • forms
  • combining
Read More

DRY custom ModelAdmin.list_display methods with a decorator

If you add a lot of custom `ModelAdmin` methods to `list_display` like I do, you know it can require a lot of repetition. Notice how adding 'checkbox' to `list_display` requires typing the method name 4 times: class ExampleAdmin(admin.ModelAdmin): list_display = ['checkbox', '__str__'] def checkbox(self, object): return '<input type="checkbox" value="%s"/>' % object.pk checkbox.short_description = mark_safe('&#x2713;') checkbox.allow_tags = True Using this decorator, the name only needs to be typed once: class ExampleAdmin(admin.ModelAdmin): list_display = ['__str__'] @add(list_display, mark_safe('&#x2713;'), 0, allow_tags=True) def checkbox(self, object): return '<input type="checkbox" value="%s"/>' % object.pk

  • admin
  • modeladmin
  • list_display
Read More

Improved Accept header middleware

A clean and simple implementation of parsing the Accept header. It places the result in request.accepted_types. Place this middleware anywhere in the chain, as all it does is add to the request object.

  • middleware
  • accept
  • header
Read More

Copy a model instance

Create a copy of a model instance. Works in model inheritance case where ``instance.pk = None`` is not good enough, since the subclass instance refers to the parent_link's primary key during save. M2M relationships are currently not handled, i.e. they are not copied. See also Django #4027.

  • model
  • copy
Read More

DebugFooter middleware with textmate links

a minor remix of simon's debug footer: <http://www.djangosnippets.org/snippets/766/> > Adds a hidden footer to the bottom of every text/html page containing a list of SQL queries executed and templates that were loaded (including their full filesystem path to help debug complex template loading scenarios). This version adds TextMate links : if you are working on your local machine and using TextMate you can click on the template paths and they will be opened in TextMate. This speeds up development time considerably ! also, this works with django 1.0 (simon's version got broke by the 'connect' refactor) update: the view function is now linked > To use, drop in to a file called 'debug_middleware.py' on your Python path and add 'debug_middleware.DebugFooter' to your MIDDLEWARE_CLASSES setting.

  • middleware
  • debug
  • textmate
Read More

render_with decorator

Automatically render your view using render_to_response with the given template name and context, using RequestContext (if you don't know what this is you probably want to be using it). For example: @render_with('books/ledger/index.html') def ledger_index(request): return { 'accounts': ledger.Account.objects.order_by('number'), }

  • render_to_response
  • requestcontext
  • decorator
  • rendering
Read More

auto generate admin.py

When you switch you django project from 0.9.6 to 1.0, you can use this script to generate admin.py automatically. You need copy cvt.py to the parent directory of your project(where your project lies) and type "python cvt.py <project> <app>". The admin.py will generated in the <project>/<app>(where it should be!). Enjoy this small work!

  • django
  • python
  • admin-interface
Read More

Imagefield with variations

Usage: from django.db import models from imagevariations.fields import ImageVariationsField, Thumbnail class Image(models.Model): name = models.CharField(max_length=20) image = ImageVariationsField(upload_to='testimages', variations=(Thumbnail,) ) How to use in templates: Use the lowercase name of the image variation class. {{ object.image.variations.thumbnail.url }} By default all image variations will use the same storage backend as the field but can be replaced per variation by setting self.storage on the variation class.

  • thumbnail
  • imagefield
  • variations
Read More

FileField with file extension whitelist

A simple FileField with a addition file extension whitelist. Raised ValidationError("Not allowed filetype!") if a filename contains a extension witch is not in the whitelist.

  • forms
  • filefield
Read More

3110 snippets posted so far.