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.
[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 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!
Allows you to include content from flatpages in class-based views. You can specify the url for the flatpage you want, or let it be determined by request.path.
Reuse blocks of template code and content as macros.
This is a small extension of https://gist.github.com/skyl/1715202 (which was based on http://djangosnippets.org/snippets/363/) to support rendering macro output into context variables.
See comments for details.
Will help you retrieve the value from a dictionary with a supplied key, or the human-readable value from a choices tuple. Works as follows:
To retrieve the value of a dict:
`{{ crime_rates_dict|getval:"Chicago" }}` <-- will return value of `crime_rates_dict["Chicago"]`
To retrieve the human-readable value from a choices tuple:
`{{ country.COUNTRIES|getval:"US" }}` <-- will return "United States" in `COUNTRIES = (("US", "United States"),)`
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.