【发布时间】:2019-06-06 18:20:03
【问题描述】:
我正在使用 django-rest-auth,我正在尝试通过覆盖表单的一种方法来修复密码重置视图中的错误。虽然我已经用不同的 django-rest-auth 表单成功地完成了类似的事情,但我无法让它在这个表单上工作。无论我做什么,都使用旧形式。
api/urls.py
from django.urls import include, path
from django.contrib.auth import views
from django.conf.urls import include, url
from django.views.generic.base import RedirectView
from .forms import SetPasswordFormCustom
from .forms import PasswordResetFormCustom
urlpatterns = [
path('password/reset/',
views.PasswordResetView.as_view(form_class=PasswordResetFormCustom),
name='rest_password_reset'),
path('rest-auth/', include('rest_auth.urls')),
path('rest-auth/registration/', include('rest_auth.registration.urls')),
path('users/', include('users.urls')),
path('reset/<uidb64>/<token>/',
views.PasswordResetConfirmView.as_view(template_name='account/password_reset_confirm.html', form_class=SetPasswordFormCustom),
name='password_reset_confirm'),
path('reset/done/', views.PasswordResetCompleteView.as_view(template_name='account/password_reset_complete.html'),
name='password_reset_complete'),
path('content/', include('lists.endpoints')),
# content is a path for lists, items etc found in the lists app
]
forms.py
from django import forms
from django.contrib.auth.forms import SetPasswordForm
from django.contrib.auth import (
authenticate, get_user_model, password_validation,
)
from django.utils.translation import gettext, gettext_lazy as _
#from django.contrib.auth import password_validation
from django.contrib.auth.forms import PasswordResetForm
UserModel = get_user_model()
class SetPasswordFormCustom(SetPasswordForm):
new_password1 = forms.CharField(
label=_("New password"),
widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'Password'}),
strip=False,
)
new_password2 = forms.CharField(
label=_("Confirm new password"),
strip=False,
widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'Confirm password'}),
help_text=password_validation.password_validators_help_text_html(),
)
class PasswordResetFormCustom(PasswordResetForm):
def get_users(self, email):
print('using custom form');
"""Given an email, return matching user(s) who should receive a reset.
This allows subclasses to more easily customize the default policies
that prevent inactive users and users with unusable passwords from
resetting their password.
"""
active_users = UserModel._default_manager.filter(**{
'%s__iexact' % UserModel.get_email_field_name(): email,
'is_active': True,
})
if not active_users:
raise forms.ValidationError(_("The e-mail address is not assigned to any user account"),
code='invalid')
return (u for u in active_users if u.has_usable_password())
所以,我想不通的是没有使用 PasswordResetFormCustom 及其对 active_users 的检查。当我请求密码重置邮件时,服务器日志中没有打印“使用自定义表单”这一行,并且没有进行检查。
我的其他自定义表单 PasswordResetConfirmView 工作正常。
没有错误,所以不是导入不正确。
知道如何在无需编辑 django-rest-auth 文件的情况下自定义 get_users 方法吗?
感谢您的帮助!
编辑:我将 yofee 的代码放入一个文件 serializers.py 中,并且我还必须在 urls.py 中修复我的 url 路径的顺序。现在可以了!
urlpatterns = [ ... path('rest-auth/password/reset/', PasswordResetView.as_view()), 路径('rest-auth/',包括('rest_auth.urls')), ... ]
重置路径必须在包含之前。
【问题讨论】:
标签: python django-forms django-rest-auth