首页 > 解决方案 > Django REST框架重置密码确认不起作用

问题描述

我正在djano使用rest_authand构建用户身份验证后端allauth。这是我api的应用程序urls.py

urlpatterns = [

    path('rest-auth/', include('rest_auth.urls')),

    path('rest-auth/registration/', include('rest_auth.registration.urls')),

    path("account/", include('allauth.urls')),
]

运行服务器后,我将rest-auth/password/reset输入电子邮件并发布。

发生错误说:

NoReverseMatch at /api/v1/rest-auth/password/reset/
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.

我发现这个问题有帮助:NoReverseMatch at /rest-auth/password/reset/

并查看了 django REST framework 官方演示:https ://django-rest-auth.readthedocs.io/en/latest/demo.html ,

我按照建议在我的 url.py 中包含了其他端点:

urlpatterns = [

    # rest framework authentication: login/logout/signup etc ...
    path('rest-auth/', include('rest_auth.urls')),

    # url endpoint for registration
    path('rest-auth/registration/', include('rest_auth.registration.urls')),

     # url endpoint to confirm email for change of password
     path('password-reset/confirm', TemplateView.as_view(template_name="password_reset_confirm.html"), name="password-reset-confirm"),

     # url link to change password (sent via email)
     path('password-reset/confirm/<uidb64>/<token>/', TemplateView.as_view(template_name="password_reset_confirm.html"),  name='password_reset_confirm'),

    # to handle different account emails (facebook, github, gmail, etc .. )
    path("account/", include('allauth.urls')),
]

html在重置 g 密码时,我还包括了我的简单登录页面。这行得通。发送一封带有确认链接的电子邮件并html加载页面。但是,在我在自定义页面中确认我的电子邮件后,会出现 django REST auth 密码重置的默认页面,我必须再次在那里输入新密码才能使密码更改生效。

现在,我宁愿只使用 Django rest 模板而不是我自己的。那么有没有办法不调用template_name="password_reset_confirm.html"并直接进入django默认值?

或者直接转到 djangorest-auth/password/reset/confirm而不是我的 custom 'password-reset/confirm',我只是为了摆脱我提到的错误而实施的。

什么是最佳解决方案?

标签: pythondjangodjango-rest-frameworkdjango-allauthdjango-rest-auth

解决方案


自己找到了答案。Daniel Roseman 是一个很好的答案,但是如果您想使用 Django rest 框架rest-auth通过电子邮件确认重置密码,以下urlpatterns是正确的:

from django.urls import include, path
from rest_auth import views

urlpatterns = [    
    path('rest-auth/', include('rest_auth.urls')),

    path('rest-auth/registration/', include('rest_auth.registration.urls')),

    path('rest-auth/password/reset/confirm/<uidb64>/<token>/', views.PasswordResetConfirmView.as_view(), name="password_reset_confirm"),

    path("account/", include('allauth.urls')),
]

在我的案例中缺少的知识是,views.PasswordResetConfirmView这是重置密码的默认视图rest-auth


推荐阅读