Today, Django 1.2 alpha1 was released and it comes with a ton of new features compared to the 1.1 branch. You can read up about them in the release notes but I want to take a quick look at probably my favorite addition: The new messaging framework. Why this and not the support for multiple databases? Well, I see myself using messaging in many more places than multiple databases ;-)
Previously the messaging system (<= 1.1) was was part of django.contrib.auth which is sometimes not what you want if all you need are messages. This has been deprecated now and replaced with a system that is rather similar to logging systems. If you want to display a message to the user you specify the message as well as a message level, which is internally represented as nothing more than an integer, so it can be filtered out depending on the system configuration by a simple greater-than comparison.
from django.contrib import messages messages.add_message(request, message.INFO, u"This is a message with the level INFO=25") messages.info(request, u"Shortcut for the above")
How these messages are stored until they can be presented to the user is completely configurable. You can go with the session object or plain old cookies and with a combination of those thanks to the MESSAGE_STORAGE setting.
The messages then get through a middleware and a context processor into your template, where they can be displayed using the "messages" variable (so be sure to not use this variable within your code or create your own little context processor that uses a different variable name). Each message comes with a set of tags ({{ message.tags }}
), that can then be used for CSS classes et al.
As with the message store, also the levels and their tags can be easily configured depending on your needs.
A big "Thank you" to Tobias McNulty and everyone else involved in this package. I'm really excited to use this in future projects. So far I only skimmed through the docs and the source code … and it is hard to resist switching some of my projects over to Django 1.2 because of all the great stuff that is in there :-)
Do you want to give me feedback about this article in private? Please send it to comments@zerokspot.com.
Alternatively, this website also supports Webmentions. If you write a post on a blog that supports this technique, I should get notified about your link 🙂