简体   繁体   中英

Error with system for email password resets

I have been having trouble creating a system for resetting passwords using email. I encountered a problem yesterday which I was unable to solve: NoReverseMatch error with password reset emails

After reading some of the relevant docs, I tried to replace the views with the class-based equivalents introduced in 1.11 as below:

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'),
]

This has introduced a new error which is not very helpful:

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

How can I get my system working?

You need to call as_view method for class based view in url.py:

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'),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM