Login

Top-rated snippets

Snippet List

Django Admin Filter __in query string

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

  • filter
  • Admin
  • ChangeList
  • query_string
Read More

Validate request params without custom form

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

  • request-validate
Read More

Decorator

[Understanding Python Decorators in 12 Easy Steps!] (http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/)

  • decorator
Read More

Controller Class for Views

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.

  • urls
  • views
Read More

Extended i18n base model

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.

  • models
  • i18n
  • metaclass
  • translated-content
Read More

convert youtube url for iframe, js api

[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]

  • url
  • youtube
  • embed
  • iframe
  • vimeo
  • video_id
Read More

Django-Celery Progress Bar backend

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!

  • progress
  • celery
Read More

Class-based view mixin for flatpages

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.

  • mixin
  • flatpages
  • class-based-views
Read More

Template {% macro %} support, with context rendering

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.

  • template
  • tag
  • macro
Read More

Retrieve human-readable value from choices tuple or value from dict

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"),)`

  • template
  • templatetag
  • choices
  • dict
  • tuple
Read More

3110 snippets posted so far.