Snippet List
RangeField and RangeWidget
These field and widget are util for to those fields where you can put a star and end values. It supports most of field types and widgets (tested with IntegerField, CharField and DateField / TextInput and a customized DateInput). **Example of use:** class FormSearch(forms.Form): q = forms.CharField(max_length=50, label=_('Search for')) price_range = RangeField(forms.IntegerField, required=False) **Example of use (with forced widget):** class FormSearch(forms.Form): q = forms.CharField(max_length=50, label=_('Search for')) price_range = RangeField(forms.IntegerField, widget=MyWidget)
- forms
- field
- widget
- range
resize to thumbnail with scale-to-fill
**So you can upload rectangular pictures but still have square thumbnails.** "Scale to **fill**" instead of the out of the box "scale to **fit**" you get with `Image.thumbnail`
- image
- pil
- thumbnail
- resize
- imagefield
Multi-level (tree-ish) navigation
An old snippet I made in my first django project. Nowadays I code menus in HTML and just use the perms proxy: https://docs.djangoproject.com/en/1.0/topics/auth/#id6 Credits, (awsome persons that helped me getting it to work efficiently and for free): * Yhg1s and waveform from #python@freenode * zendak from #django@freenode Thank you!
- menu
- navigation
- menubar
- navbar
Dynamic Backends
This allows various implementations of a common interface to be loaded. Back end modules can be specified in settings.py, and from there be loaded and treated polymorphically by an application.
- backend
- dynamic-factory
Compact P3P policy header injection middleware
This is like snippet 786, except it does it at the middleware layer, so you don't have to reference it in every view. Super simple. refer to: http://www.djangosnippets.org/snippets/786/
- middleware
- ie
- p3p
- explorer
CSRF this!
A form with built-in CSRF protection. Include CsrfCookieMiddleware in your MIDDLEWARE_SETTINGS, subclass SafeForm and off you go. See: [this django-developers post](http://groups.google.com/group/django-developers/browse_thread/thread/2c33621003992d07?hl=en) for more info. [edit] This form is actually WAY overengineered currently. Will update soon.
- forms
- csrf
SignedForm: CSRF-protect forms with a hidden token field
This form subclass helps protect against cross-site request forgery by adding a hidden field named `csrf_token` to forms. The form must be initialized with the request as a keyword argument, both with and without POST data: my_form = MySignedForm(request=request) ... my_form = MySignedForm(request.POST, request=request) Upon validation, a `PermissionDenied` exception will be raised if forgery is detected. If any security details have been overlooked in this recipe, please leave a comment.
- forms
- csrf
Custom panel showing your project svn revision on djano debug toolbar
This shows a simple way of adding your project current SVN to the django debug toolbar by creating a custom debug panel.
- debug
- debug-toolbar
- toolbar
WorldIP - access to IP database over API
The WorldIP database provides real-world geographical location. Database is more correct than [Whois records and Whois-based databases](http://www.wipmania.com/en/blog/why-worldip-data-rather-than-whois-data-examples/), that show geographic locations of network owners, and not the geographic location of Internet-connected PC or appliance itself. See more: [WIPmania.com](http://www.wipmania.com)
- ip
- geolocation
- geodjango
- worldip
Neat one off redirect
Use of a lambda function in the urlpatterns to easily redirect a url.
- redirect
Bind Administration
Binds $Model to $ModelAdmin without having to specify each binding manually. The ModelAdmins **must** have the same name as the model (as well as same case) with Admin appended. Example: from django.db import models class SomeModel(models.Model): name = models.CharField(max_length=255) class AnotherModel(models.Model): name = models.CharField(max_length=255) # bind administration bind_administration('myproject.myapp.models', 'myproject.myapp.admin')
- admin
- 1.0
- modeladmin
Decorator cache handler per view
The @cache_holding decorator works in a per-function base, you can specify a list of models or just a model and the second argument is the time in seconds to be cached. You can modify the proxy class by a keyword argument so you can do cache to all kind of things, also if you want to use a prefix as key + the hash value you can specify the prefix keyword argument.
- cache
- view
- decorator
Chaining select boxes in admin with MooTools
This code will allow you to use chained select boxes in the django automatic admin area. For example, you may have a product, then a category and subcategory. You'd like to create a product, and then choose a category, and then have a chained select box be filled with the appropriate subcategories.
- ajax
- admin
- select
- mootools
Something like list_detail generic view but returns PDF document instead
This should work as a `django.views.generic.list_detail` generic view but will produce PDF version of given template. This code is merged code from perenzo's [example](http://www.djangosnippets.org/snippets/659/) and code from `django.views.generic.list_detail` module. `pisa` package is required from (http://www.htmltopdf.org/download.html) with `html5lib` package and Reportlab Toolkit 2.1+ NOTE: this is code for Django 0.96. In Django 1.0 change in line 3: ObjectPaginator to Paginator
- generic-views
- html
- css