Skip to main content

Useful Django snippets.

Project description

A collection of reusable components to help build Django projects.


To install the latest stable release:

pip install django-utensils

To get the latest dev version, install directly form GitHub like so:

pip install -e git://

Many of the template tags require the request object. You need to add ‘django.template.context_processors.request’ (Django 1.6 and 17) or ‘django.template.context_processors.request’ (Django 1.8) to the template context processors in your settings.

To use the AddressedModel you will need to add countries_plus to your INSTALLED_APPS setting.



Generic search form that can be used on any page.

Used by viewmixins.SearchFormMixin.


Mixin that enforces unique fields on ModelForm form fields.

Must be left of ModelForm when defining the form class (see

There are two ways to list your unique fields depending on whether or not you want case insensitivity.

unique_fields = ['name', 'username']
unique_fields = ['name',  {'field': 'username', 'case_insensitive': True}]


Hidden site

By adding utensils.middleware.HiddenSiteMiddleware to your MIDDLEWARE_CLASSES you can prevent people from viewing your site unless they use a query string parameter. The parameter is not needed for subsequent visits (unless cookies are cleared). This is a quick and simple method to keep prying eyes off your staging server for example. Provide the parameter name in the settings variable HIDDEN_SITE_SECRET.

For example:

MIDDLEWARE_CLASSES += ('utensils.middleware.HiddenSiteMiddleware',)

Using the built-in development server browsing to http://localhost/ will give the message “ACCESS DENIED”. Browing to http://localhost/?whisky will succeed. Subsequent visits to http://localhost/ (no ?whisky) with the same browser will succeed until cookies are cleared or the cookie expires (currently set to a year).

View mixins

Collection of mixins for class-based views.

List view pagination

The PaginateMixin returns the paginate by setting used by the pagination template tag {% pagination %} so the Django ListView functions can use it.

You will need to add utensils.context_processors.pagination to your context processors for the template tag to work. Set settings.PAGINATION_PAGE_SIZES to control the page size, if not set the default [20, 50, 100] is used.

List view ordering

The OrderByMixin allows easy ordering of list views. By including it the template tag ({% order_by 'field_name' %}) is given sorting context variables to work with. get_queryset is overidden to make use of these and order the object list.


By including and providing success_message and/or error_message attributes on your view class, messages will be added automatically to the request objects on events suchs as valid and invalid forms and formsets, object deletion etc.


This mixin verfies the user is logged in and has the required permissions to access the view. It’s a modified version of the django-braces mixin with an added bypass. Setting permission_required = False allows you to skip the check whilst keeping the mixin in a base view used across a project, for example.


  • permission_required - the permission to check for or False to skip check

  • login_url - the login url of site

  • redirect_field_name - defaults to “next”

  • raise_exception - defaults to False - raise 403 if set to True


If a next query string parameter is preset on a post() call it is assigned to the instance success_url.


Combines MessageMixin, RedirectToNextMixin, StaffuserRequiredMixin (from django-brances), PermissionRequiredMixin to create a useful mixin that can be used on all staff views.



This view combines the pagination, order by and search mixins and adds an optional query set filter description that your templates can use.

class ActiveCustomerList(BaseListView):
    filter_description = u"Active"
    queryset =
{% block content %}
    {% if filter_description %}
      {{ filter_description|title }} customers
    {% else %}
    {% endif %}
  <!-- more template -->
{% end block %}


<h2>Active customers<h2>
<!-- more template -->


This view can be used to set the value of a field on a model instance. GET will display a template (and should be used as a confirmation page) and the value will be set on POST. The view uses django.views.generic.detail.BaseDetailView to provide get_object().

class CustomerInactiveView(SetModelFieldView):
    Ask the user if they're sure they want to make the customer inactive. If
    they confirm by submitting the form set is_active to False and save.
    model = Customer
    field = 'is_active'
    value = False
    template_name = "customers/customer_inactive.html"

If you require more control and want to introduce some logic when selecting the value or field to alter you can override get_field() and get_value() instead of setting the field and value class attributes.



The storage modules provides a way to easily store static or media files on Amazon S3.

To use you will need to update your settings with the appropriate configuration, something like this:

AWS = {
    'STATIC': {
        'location': 'static',           # AWS_LOCATION
        'querystring_auth': False,      # AWS_QUERYSTRING_AUTH
        'default_acl': 'public-read',   # AWS_DEFAULT_ACL
    'MEDIA': {
        'location': 'media',            # AWS_LOCATION
        'querystring_auth': True,       # AWS_QUERYSTRING_AUTH
        'default_acl': 'private',       # AWS_DEFAULT_ACL



Miscellaneous utils

There are utility functions in the utils module that deal with a range of things. Some are used by other parts of the library.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-utensils-0.2.tar.gz (20.4 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page