[Fixed]-Django – How to send a success message using a UpdateView CBV

37👍

To enable the messages in class based views, you need to use the SuccessMessageMixin.

from django.contrib.messages.views import SuccessMessageMixin

class NeedUpdateView(SuccessMessageMixin, UpdateView):
    ...
    success_message = 'List successfully saved!!!!'

2👍

You can also override the form_valid method like so:

from django.contrib import messages
from django.http import HttpResponseRedirect

class NeedUpdateView(UpdateView):
   ...
   def form_valid(self, form):
      messages.success(self.request, "This is my success message")
      super().form_valid(form)
      return HttpResponseRedirect(self.get_success_url())
      

Leave a comment