Skip to main content

Offline addon for django.contrib.messages

Project description

https://travis-ci.org/matthiask/django-user-messages.svg?branch=master Documentation Status

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.

  • Replace the default messages context processor with user_messages.context_processors.messages. The context processor provides both django.contrib.messages’ and django-user-messages’ messages. Note that Django 2.2’s admin app checks for the presence of the default messages context processor, so you’ll have to silence this check by adding "admin.E404" to the SILENCED_SYSTEM_CHECKS setting.

  • 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


Download files

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

Source Distribution

django-user-messages-0.6.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_user_messages-0.6.0-py2.py3-none-any.whl (11.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-user-messages-0.6.0.tar.gz.

File metadata

  • Download URL: django-user-messages-0.6.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

File hashes

Hashes for django-user-messages-0.6.0.tar.gz
Algorithm Hash digest
SHA256 acbc98f256f461f1f053349919f648c09e06b37e422a38972eb5be3e5b72171a
MD5 7962dfb7389cef8bf5b613f3a5392d52
BLAKE2b-256 c8cf83281facb675531a6b07b1703d03131bb23f046c37f0bf8c7d5e7f520ca4

See more details on using hashes here.

File details

Details for the file django_user_messages-0.6.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_user_messages-0.6.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

File hashes

Hashes for django_user_messages-0.6.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b6674f96d08f84b1c7a32cfeb12855e6a38146c6c06ab8860d04c953be3cb3f6
MD5 2ce9c40097047a636a73201f32579331
BLAKE2b-256 bba6a199a409037e3d83c0e1680d7931a988cc542226475a68a59c5c62d1c659

See more details on using hashes here.

Supported by

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