An improvement to the excellent snippet by guettli [http://djangosnippets.org/snippets/2691/](http://djangosnippets.org/snippets/2691/)
Added support for cascading relations when using model inheritance.
A hack to add __in ability to links generated in the Django Admin Filter which will add and remove values instead of only allowing to filter a single value per field. Example ?age_group__in=under25%2C25-35
When work at site api for android client, I found use form to validate user input is too complex, so I write this.
usage:
@param('param_name', 'default_value', validate_func)
def api_func(request):
# access cleaned data.
param_name = request.DATA['param_name']
JsonResponse is my class. replace with HttpResponse or whatever
I wanted to be able to share common code among a subset of views without copy-and-pasting the code or the same function into each view, so I decided to wrap a class around the views so that the common code (i.e. loading a model that each of the views would affect) can go in the __init__ of that class. I created the controller_view function above to allow the urls to access those class methods. It would be called something like this:
url(r'^someview$', controller_view(SomeController, 'someview'), name='someview'),
Where the SomeController inherits (or is structured like) the Controller class above and implements __init__ and someview as methods.
I'm new to Django so it's entirely possible I've missed something that already does this or that makes this unnecessary. If so, let me know so that I can figure out how to do this right, otherwise, hopefully this is helpful to someone else out there.
This snippet is an extension of [i18n base model for translatable content](http://djangosnippets.org/snippets/855/) so all the same usage applies.
I have extended this module in several ways to make it more fully featured.
* `I18NMixin` can be an additional (via multiple inheritance) or alternative superclass for your models juxtaposed with an `I18NModel`.
* Adds a property `_` to access the appropriate I18NModel. `trans` aliases this (or rather vice versa) for template access.
* In a call to `.filter` you can query on translated fields by wrapping those fields in a call to i18nQ. I like to import this as _ if I haven't already used that import.
* A call to I18NFieldset will return an inline for use in the builtin admin app. I like to call this inline to the assignment to inlines.
* If you need abstracted access to the I18N model from a model, I've added a property I18N referring to it.
I've been using this with great convenience and stability.
In Django 1.5 url tags require you to pass in the name of the url as a string.
So where you used to be able to do this {% url home_page %} you now have to do this {% url 'home_page' %}
Upgrading an old project can be a pain, so here is a snippet for a py file that will update all your url tags.
Just put it in a py file in your root directory and execute it.
The error you get otherwise is:
'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs.
[code]
{% get_video_urls url as url_object %}
or get object attribute:
{% get_video_urls obj "url" as url_object %}
{{ url_object }} original url
{{ url_object.video_id }} original url or video_id
{{ url_object.resource }} youtube, vimeo, None
{{ url_object.iframe_url }} original url or iframe_url
{{ url_object.js_api_url }} original url or js_api_url
[/code]
Custom form widget for rendering an autocomplete (using jQuery UI's autocomplete widget) text input in a template.
This arose from the need to have all fields asking for a state to use autocomplete, so I decided to make this.
This is a authentication backend for Django, to authenticate via Active Directory. This pulls all group information out of Active Directory and allows for multiple group memberships.
This is my POC code for a progress bar backend (for delivering JSON to some AJAX frontend) in Django using Django-Celery (with RabbitMQ). It is quite concise, yet it took me a while to get there because Celery's Documentation is IMHO rather puristic. Web development is meant to be copy-paste! Here you go!