Offline addon for django.contrib.messages
Project description
django-user-messages adds offline messaging support to Django’s messaging framework. It achieves this by allowing to save messages in the database. The user_messages.api.get_messages utility and the user_messages.context_processors.messages context processor transparently concatenate Django’s messages and our own messages in a single list, therefore existing code works without any changes and without causing database writes. django-user-messages’ functions have to be used explicitly. I consider this a feature, not a bug.
Installation
Install django-user-messages using pip into your virtualenv.
Add user_messages to INSTALLED_APPS and run migrate.
Add the user_messages.context_processors.messages message processor somewhere after the default messages processor. Django’s admin app checks for the presence of the latter so you cannot simply remove it (except if you want to silence the "admin.E404" system check).
Use user_messages.api as you would use django.contrib.messages except that you pass the user model or ID as first parameter, not the current request.
Usage
Pretty much the same as Django’s messaging framework:
from user_messages import api api.info(user, 'Hey there') api.warning(user, 'Stop this') api.error(user, 'Not nice!') # Passing the ID is also possible; the user instance does not # have to be instantiated at all: api.success(user.id, 'Yay!')
django-user-messages’ messages supports two additional features not available in Django’s messages framework:
Messages can be delivered more than once by passing deliver_once=False. These messages have to be acknowledged explicitly. django-user-messages does not contain any code to do this.
It is possible to attach additional data by passing a dictionary as meta:
api.debug(user, 'Oww', meta={ 'url': 'http://example.com', })
For convenience, our messages have the same tags and level_tag properties as Django’s messages. Meta properties are also accessible in templates:
{% if messages %} <ul class="messages"> {% for message in messages %} <li class="{{ message.tags }}".> {% if message.meta.url %}<a href="{{ message.meta.url }}">{% endif %} {{ message }} {% if message.meta.url %}</a>{% endif %} </li> {% endfor %} </ul> {% endif %}
django-user-messages’ messages are also evaluated lazily.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_user_messages-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 131ca9cfe8fb74ab981dbf194125b18e00b7b4dd897deb383848736d123d383f |
|
MD5 | b9d115a2296f74c1053563ec1fb610d6 |
|
BLAKE2b-256 | ae5c41d418bbbaa41b0353f913a2c2c5145e7737eac09bedd1a723d74759ea88 |
Hashes for django_user_messages-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1eca21f7fa17a660ad1be2987da90eed6d3d308b0d3e945fad009dd950e9ddd0 |
|
MD5 | 1f25509d4264d6b4d8bc76a2f9f8700e |
|
BLAKE2b-256 | f1cda75d065eb5ccb1e8b29d0e0d4116b433d885898657c37145fdee56c5c29e |