python - Django REST框架重置密码确认不起作用
问题描述
我正在djano
使用rest_auth
and构建用户身份验证后端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'
,我只是为了摆脱我提到的错误而实施的。
什么是最佳解决方案?
解决方案
自己找到了答案。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
推荐阅读
- vba - 在字符串中查找特定数字
- c# - Unity System.InvalidOperationException: '没有找到匹配的成员数据
- java - 使用 Lambda 部分展平 Java 中的嵌套集合
- excel - 为什么 Excel VBA 不能在 Word 文档中查找和替换
- c - 如何从父进程向子进程发出信号?
- bash - -mtime 在 bash 脚本中是什么意思?
- wpf - WPF 进度条样式文本
- mongodb - Mongoose Schema 规划:使用 ObjectID 引用还是使用类型数组:[otherSchema]?
- node.js - 安装 Foundation 模板,未安装节点模块
- docker - 如何在 Docker 容器中运行 AWS Lambda 层?