我正在使用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可以正常工作。
没有错误,因此并不是导入不正确。
有什么主意我可以自定义get_users方法而无需编辑django-rest-auth文件吗?
感谢您的帮助!
编辑:我将yofee的代码放入了一个serializers.py文件中,而且我还不得不在urls.py中修复我的url路径的顺序。现在可以了!
urlpatterns = [ ... path('rest-auth / password / reset /',PasswordResetView.as_view()), path('rest-auth /',include('rest_auth.urls')), ... ]
重置路径必须在包含之前。
答案 0 :(得分:1)
解决方案应该是
Bucket Sort Aggregation
PasswordResetFormCustom
的{{1}}(不是Django的)示例:
rest_auth