139
141
============== ===========
141
143
To change the default tags for a message level (either built-in or custom),
142
set the `MESSAGE_TAGS`_ setting to a dictionary containing the levels
144
set the :setting:`MESSAGE_TAGS` setting to a dictionary containing the levels
143
145
you wish to change. As this extends the default tags, you only need to provide
144
146
tags for the levels you wish to override::
285
289
use one of the ``add_message`` family of methods. It does not hide failures
286
290
that may occur for other reasons.
292
Adding messages in Class Based Views
293
------------------------------------
295
.. versionadded:: 1.6
297
.. class:: views.SuccessMessageMixin
299
Adds a success message attribute to
300
:class:`~django.views.generic.edit.FormView` based classes
302
.. method:: get_success_message(cleaned_data)
304
``cleaned_data`` is the cleaned data from the form which is used for
307
**Example views.py**::
309
from django.contrib.messages.views import SuccessMessageMixin
310
from django.views.generic.edit import CreateView
311
from myapp.models import Author
313
class AuthorCreate(SuccessMessageMixin, CreateView):
315
success_url = '/success/'
316
success_message = "%(name)s was created successfully"
318
The cleaned data from the ``form`` is available for string interpolation using
319
the ``%(field_name)s`` syntax. For ModelForms, if you need access to fields
320
from the saved ``object`` override the
321
:meth:`~django.contrib.messages.views.SuccessMessageMixin.get_success_message`
324
**Example views.py for ModelForms**::
326
from django.contrib.messages.views import SuccessMessageMixin
327
from django.views.generic.edit import CreateView
328
from myapp.models import ComplicatedModel
330
class ComplicatedCreate(SuccessMessageMixin, CreateView):
331
model = ComplicatedModel
332
success_url = '/success/'
333
success_message = "%(calculated_field)s was created successfully"
335
def get_success_message(self, cleaned_data):
336
return self.success_message % dict(cleaned_data,
337
calculated_field=self.object.calculated_field)
288
339
Expiration of messages
289
340
======================
322
A few :doc:`Django settings </ref/settings>` give you control over message
373
A few :ref:`settings<settings-messages>` give you control over message
328
Default: ``messages.INFO``
330
This sets the minimum message that will be saved in the message storage. See
331
`Message levels`_ above for more details.
333
.. admonition:: Important
335
If you override ``MESSAGE_LEVEL`` in your settings file and rely on any of
336
the built-in constants, you must import the constants module directly to
337
avoid the potential for circular imports, e.g.::
339
from django.contrib.messages import constants as message_constants
340
MESSAGE_LEVEL = message_constants.DEBUG
342
If desired, you may specify the numeric values for the constants directly
343
according to the values in the above :ref:`constants table
344
<message-level-constants>`.
349
Default: ``'django.contrib.messages.storage.fallback.FallbackStorage'``
351
Controls where Django stores message data. Valid values are:
353
* ``'django.contrib.messages.storage.fallback.FallbackStorage'``
354
* ``'django.contrib.messages.storage.session.SessionStorage'``
355
* ``'django.contrib.messages.storage.cookie.CookieStorage'``
357
See `Storage backends`_ for more details.
364
{messages.DEBUG: 'debug',
365
messages.INFO: 'info',
366
messages.SUCCESS: 'success',
367
messages.WARNING: 'warning',
368
messages.ERROR: 'error',}
370
This sets the mapping of message level to message tag, which is typically
371
rendered as a CSS class in HTML. If you specify a value, it will extend
372
the default. This means you only have to specify those values which you need
373
to override. See `Displaying messages`_ above for more details.
375
.. admonition:: Important
377
If you override ``MESSAGE_TAGS`` in your settings file and rely on any of
378
the built-in constants, you must import the ``constants`` module directly to
379
avoid the potential for circular imports, e.g.::
381
from django.contrib.messages import constants as message_constants
382
MESSAGE_TAGS = {message_constants.INFO: ''}
384
If desired, you may specify the numeric values for the constants directly
385
according to the values in the above :ref:`constants table
386
<message-level-constants>`.
388
SESSION_COOKIE_DOMAIN
389
---------------------
393
The storage backends that use cookies -- ``CookieStorage`` and
394
``FallbackStorage`` -- use the value of :setting:`SESSION_COOKIE_DOMAIN` in
395
setting their cookies. See the :doc:`settings documentation </ref/settings>`
396
for more information on how this works and why you might need to set it.
398
.. _Django settings: ../settings/
376
* :setting:`MESSAGE_LEVEL`
377
* :setting:`MESSAGE_STORAGE`
378
* :setting:`MESSAGE_TAGS`