Eivind Uggedal

Efficient and Lightweight Notices in Django

Drawing inspiration from Merb I wrote a lightweight messaging app for Django. My solution uses a normal GET query parameter containing the base64 and urlencoded message. django-notices supports sending messages to the next page when a HTTP redirect is used. The developer API thus looks like this:

from notices import HttpResponseRedirectWithNotice
from django.shortcuts import render_to_response

def add_counter(req, number):
    if number > 0:
        notice = "Created counter with %s" % number
        return HttpResponseRedirectWithNotice("/", success=notice)
        return render_to_response('form.html', {
            'error_message': "Counters have to be positive"

To handle these messages in a template one could do something like this:

{% if notices %}
  <ul class="notices">
    {% for type, notice in notices.items %}
      <li class="{{ type }}">{{ notice }}</li>
    {% endfor %}
{% endif %}

By default HttpResponseRedirectWithNotice supports notices with the type of success, error, and notice. This can be changed in settings.py:

NOTICE_TYPES = ("success", "failure")

An alternative to django-notices are the bundled messages system in django.contrib.auth. My main grief with this solution is that it only supports one type of message to be sent. Another problem is that is uses the database, which results in one extra query for each page view.

There are cookie based solutions inspired by Rails available for Django, but I don't like using cookies to handle such state.