系统重置电子邮件密码错误

时间:2018-02-11 08:45:49

标签: python django forgot-password

我在创建使用电子邮件重置密码的系统时遇到了问题。我昨天遇到了一个我无法解决的问题: NoReverseMatch error with password reset emails

在阅读了一些相关文档之后,我尝试用1.11中引入的基于类的等价替换视图,如下所示:

urls.py:

from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r'^$', auth_views.login, name='login'),
    url(r'^logout/$', auth_views.logout, name='logout'),

    ## more irrelevant urls here ##
    url(r'^password/reset/done/$', auth_views.PasswordResetDoneView, name='password_reset_done'),
    url(r'^password/reset/$', auth_views.PasswordResetView, name='password_reset'),
    url(r'^password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', auth_views.PasswordResetConfirmView, name='password_reset_confirm'),
    url(r'^password/reset/complete/$', auth_views.PasswordResetCompleteView, name='password_reset_complete'),
]

这引入了一个不太有用的新错误:

Internal Server Error: /password/reset/
Traceback (most recent call last):
  File "C:\python\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\python\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\python\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given
[11/Feb/2018 12:35:38] "GET /password/reset/ HTTP/1.1" 500 62168

如何让我的系统正常运行?

1 个答案:

答案 0 :(得分:2)

您需要在url.py中为基于类的视图调用as_view方法:

url(r'^password/reset/done/$', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
url(r'^password/reset/$', auth_views.PasswordResetView.as_view(), name='password_reset'),
url(r'^password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
url(r'^password/reset/complete/$', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),