带有自定义用户模型的 PasswordChangeForm [英] PasswordChangeForm with Custom User Model
问题描述
我最近通过继承抽象用户实现了我自己的用户模型.
I recently implemented my own user model by subclassing abstract user.
class NewUserModel(AbstractUser):
执行此操作后,PasswordChangeForm
停止工作.我通过覆盖 class Meta:
模型字段修复了 UserCreationForm
中的问题.但是,ChangePasswordForm
没有指定模型,我看不出它不能与新用户模型一起使用的任何原因.
After I did this the PasswordChangeForm
stopped working. I fixed the issue in the UserCreationForm
by overriding the class Meta:
model field. However, the ChangePasswordForm
doesn't specify a model and I can't see any reason why it shouldn't work with the new user model.
views.py
class PasswordChangeView(LoginRequiredMixin, FormView):
template_name = 'change_password.html'
form_class = PasswordChangeForm
def get_form_kwargs(self):
kwargs = super(PasswordChangeView, self).get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
推荐答案
一整天都在努力实现这一目标.最终我发现用 FBV 实现它非常简单:
Just spent most of the day trying to achieve this. Eventually I found out it was pretty simple to implement it with a FBV:
@login_required
def UpdatePassword(request):
form = PasswordChangeForm(user=request.user)
if request.method == 'POST':
form = PasswordChangeForm(user=request.user, data=request.POST)
if form.is_valid():
form.save()
update_session_auth_hash(request, form.user)
return render(request, 'user/password.html', {
'form': form,
})
这篇关于带有自定义用户模型的 PasswordChangeForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!