Show message after changing password?

I am using the default password reset mechanism provided by django.

I use post_change_redirect so that the submitted form returns immediately to my settings page, however I would like to show a message to convince the user that the operation was successful. How can I determine if I am arriving in the settings window as a result of a successful password change and add a message about it?

+5
source share
2 answers

I would not recommend checking in the settings whether the user arrived by changing the password. I think that ideally the whole logic of password changes is contained in the same place. This makes it easier to find the logic and does not require the settings view to be aware of viewing the password change (so you can easily change the logic to redirect the user to another location).

It's best to write your own look based on PasswordChangeForm instead of using the built-in password_change view. With this approach, you can use the message structure to display a successful message. (You also need to enable the message framework and put its markup in your views.)

For example, if you want to display a simple message and redirect back to a URL template called 'settings' , you can write a representation like this:

 from django.contrib import messages from django.contrib.auth.forms import PasswordChangeForm from django.core.urlresolvers import reverse_lazy from django.views.generic import FormView class PasswordChangeView(FormView): template_name = 'registration/password_change_form.html' form_class = PasswordChangeForm success_url = reverse_lazy('settings') def get_form_kwargs(self): kwargs = super(PasswordChangeView, self).get_form_kwargs() kwargs['user'] = self.request.user return kwargs def form_valid(self, form): form.save() messages.success(self.request, "Your password has been changed.") return super(FormView, self).form_valid(form) 

We hope that the password_change representation will be made based on the class in the future, allowing the same behavior with even fewer templates.

+7
source

You can django.contrib.auth.views.PasswordChangeView subclass django.contrib.auth.views.PasswordChangeView to add a message using the Django message infrastructure when the user successfully django.contrib.auth.views.PasswordChangeView their password.

In views.py :

 from django.contrib import messages from django.contrib.auth.views import PasswordChangeView from django.urls import reverse_lazy class CustomPasswordChangeView(PasswordChangeView): # Optional (default: 'registration/password_change_form.html') template_name = 'myapp/my_password_change_form.html' # Optional (default: 'reverse_lazy('password_change_done')') success_url = reverse_lazy('settings') def form_valid(self, form): messages.success(self.request, 'Your password has been changed.') return super().form_valid(form) 

Then in your application urls.py :

 from django.conf.urls import url import myapp.views as views urlpatterns = [ url(r'^settings/password/change/$', views.CustomPasswordChangeView.as_view(), name='password_change'), # ... ] 

This answer improves the answer provided by engnoid in the following ways:

  • The form behaves almost identically to the default password mechanism.
  • Less code repetition.
0
source

Source: https://habr.com/ru/post/1446045/


All Articles