| 1 | from django.core.exceptions import ImproperlyConfigured |
| 2 | from django.views.generic import FormMixin |
| 3 | from django.contrib import messages |
| 4 | |
| 5 | |
| 6 | class 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 |