#35850: Default success messages for CreateView/UpdateView/DeleteView/...
-------------------------------------+-------------------------------------
     Reporter:  Willem Van Onsem     |                     Type:  New
                                     |  feature
       Status:  new                  |                Component:  Generic
                                     |  views
      Version:  5.0                  |                 Severity:  Normal
     Keywords:  translations,        |             Triage Stage:
  success-message, views             |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 Hi,

 I'm wondering if it would make sense to provide default success messages
 for the `CreatView`/`UpdateView`/`DeleteView` that will *not* be enabled
 as long as the `SuccessMessageMixin` is not enabled, but are when that is
 the case.

 In order to do that we would have to split the `SuccessMessageMixin` to
 trick the MRO in overriding the message. So that would look like:

 {{{
 class BaseSuccessMessageMixin:
     success_message = ""


 class SuccessMessageMixin(BaseSuccessMessageMixin):
     """
     Add a success message on successful form submission.
     """

     def form_valid(self, form):
         response = super().form_valid(form)
         success_message = self.get_success_message(form.cleaned_data)
         if success_message:
             messages.success(self.request, success_message)
         return response

     def get_success_message(self, cleaned_data):
         return self.success_message % cleaned_data
 }}}

 then we can define a `DeleteView` like:

 {{{
 class DeleteView(SingleObjectTemplateResponseMixin,
 BaseSuccessMessageMixin, BaseDeleteView):
     success_message = _('The object has been deleted successfully')
 }}}

 This would make a simple message easily translatable: people can just add
 a custom translation and the other ones are retained.

 The `BaseSuccessMessageMixin` is necessary to make sure that if someone
 inherits the `SuccessMessageMixin`, it will *not* override the
 `success_message` once again by the empty string.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35850>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701929f5db357-a2070e9b-7b99-4579-82ee-c7dc22f17228-000000%40eu-central-1.amazonses.com.

Reply via email to