Storage
Storage can be access using django.contrib.message.get_messages
method.
For example:
from django.contrib.messages import get_messages
storage = get_messages(request)
Attributes
- level
Minimum message level. Can be modified to specify custom minimum message level. For example:
from django.contrib import messages messages.set_level(request, messages.DEBUG) # is the same as storage = messages.get_messages(request) storage.level = messages.DEBUG
- used
Indicates the storage was used. You may set to
False
to avoid the message deletion procedure.from django.contrib import messages storage = messages.get_messages(request) for message in storage: print(message) storage.used = False
See also
More information in the django’s messages framework docs https://docs.djangoproject.com/en/3.1/ref/contrib/messages/#expiration-of-messages
Python Access
Storage object provide many syntax based access that is similar to other Python collections. All access via those interfaces is for unread messages, and some of them also mark them as read.
Example |
Read |
Explanation |
---|---|---|
|
✅ |
Retrieve messages lazily |
|
✅ |
Get specific message from storage |
|
✅ |
Get subset of messages (slice) |
|
❌ |
Check if there are unread messages |
|
❌ |
Get count if unread messages |
|
❌ |
Check if a message exists and is unread |
|
❌ |
Check if an unread message with that text exists |
|
❌ |
Check if an unread message with that level exists |
|
✅ |
Get all messages, divided by comma |
|
❌ |
Get all messages, divided by comma |
Warning
When iterating over storage, the marking of messages as read is done after iteration is over. When iteration is complete all unread messages will be marked as read, whether they were returned during iteration or not.
Methods
- get_queryset()
Get queryset of all messages for that request.
- get_unread_queryset()
Get queryset of unread messages for that request.
- add(level, message, extra_args)
Add a new message to the storage.
- update(response)
Perform deleting procedure manually.