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!
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.
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.
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!
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.
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('✓')
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('✓'), 0, allow_tags=True)
def checkbox(self, object):
return '<input type="checkbox" value="%s"/>' % object.pk
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.
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.
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.
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'),
}
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!
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.
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.
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.