mirror of
https://github.com/cookiecutter/cookiecutter-django.git
synced 2024-11-22 17:47:08 +03:00
Using SuccessMessageMixin to pass the message to django template instead of overriding form_valid
This commit is contained in:
parent
9903cb9fcd
commit
d358fc948d
|
@ -1,6 +1,6 @@
|
|||
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,23 +18,18 @@ 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})
|
||||
|
||||
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()
|
||||
|
||||
|
@ -47,4 +42,4 @@ class UserRedirectView(LoginRequiredMixin, RedirectView):
|
|||
return reverse("users:detail", kwargs={"username": self.request.user.username})
|
||||
|
||||
|
||||
user_redirect_view = UserRedirectView.as_view()
|
||||
user_redirect_view = UserRedirectView.as_view()
|
Loading…
Reference in New Issue
Block a user