Last active
February 23, 2017 09:14
-
-
Save avillp/fa89c93192db298890b34cc8346b21db to your computer and use it in GitHub Desktop.
Example of Django class based password_change view with a custom form inheritance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# views.py | |
from django.contrib.auth.views import password_change | |
from django.views.generic import FormView | |
from backoffice.mixins import PermissionsMixin, ContextMixin | |
class EditPasswordView(ContextMixin, PermissionsMixin, FormView): | |
""" | |
Allows the user to edit their password. | |
""" | |
template_name = 'backoffice/profile/edit-password.html' | |
form_class = EditPasswordForm | |
success_url = reverse_lazy('password_change_done') | |
permissions = ('community', ) | |
def get_form_kwargs(self): | |
kwargs = super().get_form_kwargs() | |
kwargs.update({ | |
'user': self.request.user, }) | |
return kwargs | |
def form_valid(self, form): | |
return password_change(self.request, post_change_redirect=self.success_url) | |
def render_to_response(self, context, **response_kwargs): | |
""" | |
Sends all the information needed to render the view to Django's password_change function. | |
""" | |
return password_change(self.request, self.template_name, password_change_form=self.form_class, | |
extra_context=context) | |
# forms.py | |
from backoffice.mixins import FormMixin | |
from django.contrib.auth.forms import PasswordChangeForm | |
class EditPasswordForm(FormMixin, PasswordChangeForm): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment