Using SuccessMessageMixin to pass the message to django template instead of overriding form_valid
-from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import LoginRequiredMixin
+from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.generic import DetailView, RedirectView, UpdateView
@@ -18,10 +18,11 @@ class UserDetailView(LoginRequiredMixin, DetailView):
user_detail_view = UserDetailView.as_view()
-class UserUpdateView(LoginRequiredMixin, UpdateView):
+class UserUpdateView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
model = User
fields = ["name"]
+ success_message = _("Information successfully updated")
def get_success_url(self):
return reverse("users:detail", kwargs={"username": self.request.user.username})
@@ -29,12 +30,6 @@ class UserUpdateView(LoginRequiredMixin, UpdateView):
def get_object(self):
return self.request.user
- def form_valid(self, form):
- messages.add_message(
- self.request, messages.INFO, _("Information successfully updated")
- )
- return super().form_valid(form)
-
user_update_view = UserUpdateView.as_view()
