Keeping the users of your application aware of what is going on makes a huge difference in the user experience. If there is something users hate more than slow applications, it is applications that does not communicate with them.
– The user clicks in a save button.
– Nothing happens.
– So, did it save the data or not?
– User reaction after a couple of (mili)seconds: *Click!* *Click!* *Click!* *Click!*
Let’s make our users more confident and comfortable, shall we?
By default, a brand new Django project already comes with the messages framework installed. If you did not change anything in relation to the messages framework, just skip to the next section. Otherwise, set it up:
- MIDDLEWARE or MIDDLEWARE_CLASSES in older versions:
Message Levels and Tags
|Constant||Level||Tag (for CSS)||Purpose|
|DEBUG||10||debug||Development-related messages that will be ignored (or removed) in a production deployment|
|INFO||20||info||Informational messages for the user|
|SUCCESS||25||success||An action was successful|
|WARNING||30||warning||A failure did not occur but may be imminent|
|ERROR||40||error||An action was not successful or some other failure occurred|
By default, Django will only display messages with level greater than 20 (INFO). If you want to display DEBUG messages:
Or if you are running into circular imports, you can add the constant value directly:
You have two ways to use it. If you are using the built-in message levels (which more the most cases are more than enough):
And then in the template:
If the success message was added, the output would be something like that:
You can also pass extra tags to the message:
Extra: Bootstrap Snippet
And then to use it, add messages.html to your base.html template: