Login

All snippets

Snippet List

Add GET parameter tag

This is custom tag I wrote for myself for solving situations when you have filter form and page numbers in the same page. You want to change ?page=.. or add it if it doesn't exist to save filter form data while moving through pages. Usage: place this code in your application_dir/templatetags/add_url_parameter.py In template: {% load add_get_parameter %} <a href="{% add_get_paramater param1='const_value',param2=variable_in_context %}"> Link with modified params </a> It's required that you have 'django.core.context_processors.request' in TEMPLATE_CONTEXT_PROCESSORS URL: [http://django.mar.lt/2010/07/add-get-parameter-tag.html](http://django.mar.lt/2010/07/add-get-parameter-tag.html)

  • tag
  • parameters
  • GET
  • add
  • add_get_parameter
Read More

Partial Tag

Lets you include another template and pass in named variables. Use like: {% partial field_template field=form.city %} If no key is specified, it will use "item" instead. You may pass in multiple variables as comma-separated key=value pairs.

  • context
  • partial
  • include
  • ssi
Read More

View Redirect Decorators

Temporary and permanent view redirect decorators. Simplifies views that always redirect to a specific or configured url. You can use the reverse() function (or any other runtime-required lookup) via `lambda` to define the redirect url.

  • view
  • decorator
  • redirects
Read More

JSON View Decorator

Django JSON view decorator. Dumps the object returned from the view function. Allows you to customize the JSON dumps() function using the available keyword arguments available from the simplejson module. By default, indents the output with 4 characters.

  • json
  • decorator
Read More

Middleware to move tags <script> to the bottom

This middleware makes the page load faster because it moves all tags <script> to the end of document. When a tag <script> loads, it blocks parallel loading, so, the best practice is load them after load CSS and images. To use it, just put in a file and add to your setting MIDDLEWARE_CLASSES.

  • performance
  • script
  • otimization
Read More

Aggregation class "Count" with Case

Use it like below: totals = MyClass.aggregate( is_enabled_yes=CountCase('is_enabled', when=True), is_enabled_no=CountCase('is_enabled', when=False), count_if=CountCase('id', case="another_field in ('a','b')", when=True), )

  • count
  • annotate
  • aggregation
Read More

Aggregation class "Sum" with Case

Just use it like below: from downloaded_file import SumCase MyClass.objects.aggregate( sum1=SumCase('salary', case='salary < 4', when=True), sum1=SumCase('salary', case='type', when='director'), )

  • annotate
  • aggregation
  • sum
Read More

Add a CSS class to every field indicating what kind of field it is

The admin site uses a CSS class for each kind of field so that they can be styled easily. This snippet gives ModelForms a similar feature. See also: [James Bennett's explanation of formfield_callback](http://stackoverflow.com/questions/660929/how-do-you-change-the-default-widget-for-all-django-date-fields-in-a-modelform/661171#661171)

  • css
  • field
  • widget
  • modelform
  • callback
Read More

While loop template tag

The missing `while` template tag. Built on top of http://djangosnippets.org/snippets/2093/, it also supports `break` and `continue` out of the box.

  • template
  • templatetag
  • loop
  • while
Read More

Image model with thumbnail

A relatively simple Photo model which generates its own thumbnail when saved. A default size is specified as `thumb_size` in the `save()` arguments. Other thumbnailing strategies don't save the thumbnail dimensions, and since the actual dimensions of the thumbnail created by PIL's `thumbnail` method are somewhat non-deterministic, it is difficult to create an `img` tag with proper height and width attributes. This approach makes that task simple. This approach works well if only one thumbnail size is required. It could easily be adapted to support two or three thumbnail sizes, but adding more sizes would quickly get unwieldy. This was adapted from http://biohackers.net/wiki/Django1.0/Thumbnail

  • image
  • thumbnails
  • thumbs
Read More

Continuing and breaking from loops in Django templates

This snippet makes Django templates support `break` and `continue` in loops. It is actually more powerful than the respective Python statements as it allows breaking and continuing from an outer loop, not just the innermost. `break` and `continue` are implemented as template filters, with the input value being the loop variable. For example, to break from the current `for` loop use `forloop|break`, and to continue from the next outer loop use `forloop.parentloop|continue`. The implementation monkeypatches Django (specifically Nodelist and ForNode) and has been tested on v1.2 with Python 2.6.

  • template
  • loop
  • break
  • continue
Read More

Get actual child model in multi-table inheritance

A common problem (it hit the Django mailinglist a couple of times) is that if you get `models.Topping.objects.all()`, you get a list of toppings, although they stand for other classes such as `SalamiTopping` or `CheeseTopping`. If you need the actual object, just derive `Topping` from `PolymorphicModel`, and say `topping.actual_instance`. This will give you e.g. a `SalamiTopping`. Sometimes you just want to check for the actual class. You can get it by saying `topping.content_type.model_class()`. There is a slight performance impact when creating objects because they have to be saved twice. NEWS: A good alternative to this approach is the [InheritanceManager](https://github.com/carljm/django-model-utils/blob/master/README.rst).

  • models
  • child-model
  • multi-table-inheritance
  • polymorphy
Read More

3109 snippets posted so far.