Kaydet (Commit) 24fcca6b authored tarafından Rémy HUBSCHER's avatar Rémy HUBSCHER Kaydeden (comit) Baptiste Mispelon

Fixed #21591 -- Added documentation about contrib.messages.get_messages.

Refs https://code.djangoproject.com/ticket/21591
Thanks to track user merb for the report.
üst 9922ed46
...@@ -177,8 +177,9 @@ used tags (which are usually represented as HTML classes for the message):: ...@@ -177,8 +177,9 @@ used tags (which are usually represented as HTML classes for the message)::
Displaying messages Displaying messages
------------------- -------------------
.. function:: get_messages(request)
In your template, use something like:: **In your template**, use something like::
{% if messages %} {% if messages %}
<ul class="messages"> <ul class="messages">
...@@ -212,6 +213,25 @@ is a mapping of the message level names to their numeric value:: ...@@ -212,6 +213,25 @@ is a mapping of the message level names to their numeric value::
</ul> </ul>
{% endif %} {% endif %}
**Outside of templates**, you can use
:func:`~django.contrib.messages.get_messages`::
from django.contrib.messages import get_messages
storage = get_messages(request)
for message in storage:
do_something_with_the_message(message)
For instance, you can fetch all the messages to return them in a
:ref:`JSONResponseMixin <jsonresponsemixin-example>` instead of a
:class:`~django.views.generic.base.TemplateResponseMixin`.
:func:`~django.contrib.messages.get_messages` will return an
instance of the configured storage backend.
The ``Message`` class The ``Message`` class
--------------------- ---------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment