Skip to content

Instantly share code, notes, and snippets.

@avillp
Last active February 23, 2017 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avillp/fa89c93192db298890b34cc8346b21db to your computer and use it in GitHub Desktop.
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.
# 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