You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

27 lines
957 B

  1. from django.conf import settings
  2. from django.contrib.messages.storage import default_storage
  3. class MessageMiddleware(object):
  4. """
  5. Middleware that handles temporary messages.
  6. """
  7. def process_request(self, request):
  8. request._messages = default_storage(request)
  9. def process_response(self, request, response):
  10. """
  11. Updates the storage backend (i.e., saves the messages).
  12. If not all messages could not be stored and ``DEBUG`` is ``True``, a
  13. ``ValueError`` is raised.
  14. """
  15. # A higher middleware layer may return a request which does not contain
  16. # messages storage, so make no assumption that it will be there.
  17. if hasattr(request, '_messages'):
  18. unstored_messages = request._messages.update(response)
  19. if unstored_messages and settings.DEBUG:
  20. raise ValueError('Not all temporary messages could be stored.')
  21. return response