#33587: Improve SuccessMessageMixin working with BaseDeleteView
--------------------------------------------+------------------------
               Reporter:  Chris Chapman     |          Owner:  nobody
                   Type:  Bug               |         Status:  new
              Component:  contrib.messages  |        Version:  4.0
               Severity:  Normal            |       Keywords:
           Triage Stage:  Unreviewed        |      Has patch:  0
    Needs documentation:  0                 |    Needs tests:  0
Patch needs improvement:  0                 |  Easy pickings:  0
                  UI/UX:  0                 |
--------------------------------------------+------------------------
 Currently a developer is blocked from easily using values from the object
 before deleting it. This is because `BaseDeleteView.form_valid` is where
 the object is deleted, but this is called before constructing the success
 message. This could be easily improved if `SuccessMessageMixin.form_valid`
 method was slightly modified. The following suggestion just switches the
 order of the first two lines in the method:

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

 }}}

 This change would allow the following to work:
 {{{
 MyDeleteView(SuccessMessageMixin, DeleteView):
     """Delete object and give user feedback on success."""

     success_message = "Successfully deleted %(name)s"
     ...

     def get_success_message(self, cleaned_data):
         data = {**cleaned_data, "name": str(self.object)}
         return super().get_success_message(data)
 }}}

 Just as before, the message will only be applied if the call to
 `super().form_valid(form)` is successful.
 This change would allow access to the object as typically expected with
 any class inheriting from `SingleObjectMixin`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33587>
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/0107017fa02f430e-0e589be7-8c16-4af2-ab6b-cf781e1b70d4-000000%40eu-central-1.amazonses.com.

Reply via email to