Login

Snippets by asfaltboy

Snippet List

Filter changelist by a numeric field using a number of common value ranges

## How to use Use this [admin filter](https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter) together with a numeric field to allow filtering changlist by field values range (in this case, age groups): For example, to group customers by age groups: class Customer(models.Model): # ... age = models.IntegerField() age.list_lookup_range = ( (None, _('All')), ([0, 2], '0-2'), ([2, 4], '2-4'), ([4, 18], '4-18'), ([18, 65], '18-65'), ([65, None], '65+'), )) class CustomerAdmin(admin.ModelAdmin): list_filter = [('age', ValueRangeFilter), ] ## Inspiration [This snippet](https://djangosnippets.org/snippets/587/) (for django < 1.4) inspired me to make this work for newer django versions.

  • filter
  • admin
  • field
  • range
Read More

Template tag to render collections.Counter as an html table

Render a given instance of collections.Counter into a 2 column html table. Optionally accepts `column_title` keyword argument which sets the table key column header. Usage: {% counter_table event_counter column_title='event type' %} The above will render the a table from the `event_counter` variable with the first (key) column set to "event type". See below for an example template (i.e `counter_table.html`) {% load i18n %} <table> <thead> <tr> <th>{{column_title|capfirst}}</th> <th>{% trans "count"|capfirst %}</th> </tr> </thead> <tbody> {% for key, count in most_common %} <tr> <td>{{key}}</td> <td>{{count}}</td> </tr> {% endfor %} </tbody> <tfoot> <tr> <td>{% trans "total"|capfirst %}</td> <td>{{total_count}}</td> </tr> </tfoot> </table>

  • html
  • table
  • inclusion-tag
  • collections.Counter
  • Counter
Read More

ContentType template filter

Custom template filter to retrieve a content type of a given model instance. Useful for ModelForms which want to set the content_type field (i.e: GenericForeignKey). ### A usage example: {% load helpers %} {% with instance|content_type as ctype %} <input type="hidden" name="content_type" value="{{ ctype.pk }}"> {% endwith %} Original idea from [this stackoverflow answer] [1] [1]: http://stackoverflow.com/a/12807458/484127

  • template
  • filter
  • tag
  • contenttypes
  • contenttype
  • GenericForeignKey
Read More

asfaltboy has posted 4 snippets.