Ticket #16319: SuccessMessageMixin.diff

File SuccessMessageMixin.diff, 934 bytes (added by martinogden, 13 years ago)

Add SuccessMessageMixin to django.contrib.messages.views

  • django/contrib/messages/views.py

     
     1from django.core.exceptions import ImproperlyConfigured
     2from django.views.generic import FormMixin
     3from django.contrib import messages
     4
     5
     6class SuccessMessageMixin(FormMixin):
     7    """
     8    A mixin that add a success message when a form is completed
     9    """
     10    success_message = None
     11
     12    def is_valid(self, form):
     13        success_message = self.get_success_message()
     14        if success_message:
     15            messages.success(self.request, success_message)
     16        return super(SuccessMessageMixin, self).is_valid(form)
     17
     18    def get_success_message(self):
     19        if has_attr(self, 'object'):
     20            return self.success_message % self.object.__dict__
     21        return self.success_message
Back to Top