python - django-rest-auth:密码重置功能问题

标签 python django django-rest-framework django-allauth django-rest-auth

我一直在尝试使用 django-rest-auth 在 DRF 中设置密码重置功能。早些时候,我收到错误TemplateDoesNotExist:registration/password_reset_email.html,我通过添加以下代码解决了该问题

serializer.py

from rest_auth.serializers import PasswordResetSerializer
from allauth.account.forms import ResetPasswordForm  

class PasswordSerializer(PasswordResetSerializer):
    password_reset_form_class = ResetPasswordForm

settings.py

REST_AUTH_SERIALIZERS = {
    'PASSWORD_RESET_SERIALIZER': 'api.serializers.PasswordSerializer',
}

但是,现在我遇到了另一个问题 - “NoReverseMatch:未找到 'account_reset_password_from_key' 的反向。'account_reset_password_from_key' 不是有效的 View 函数或模式名称。”。并且还没有找到任何解决方案或解决方法。

如有任何帮助,我们将不胜感激。

最佳答案

所以,我终于可以使用密码重置功能了。事情是这样的 -

我们的 urls.py 中只需要一个 URL -

urlpatterns = [
url(r'^account/', include('allauth.urls')),  
url(r'^rest-auth/', include('rest_auth.urls')),  

# This is the only URL required for BASIC password reset functionality.
# This URL creates the confirmation link which is sent via e-mail. All of the rest
# password reset features get their reverse lookup via django-allauth and django-rest-auth.
url(r'^password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', TemplateView.as_view(),  name='password_reset_confirm'), 

url(r'^rest-auth/registration/account-confirm-email/(?P<key>[-:\w]+)/$', allauthemailconfirmation,
    name="account_confirm_email"),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls'), name='account_signup'),  
]

使用此 URL 配置首先引发 TemplateDoesNotExist at/api/rest-auth/password/reset/ 错误。经过大量调试后,我发现问题是由位于 Django Admin 模板目录下的模板 - registration/password_reset_email.html 引发的。发生这种情况是由于我正在使用的另一个 Django 应用程序,它禁用了 django 管理应用程序。

因此,在 INSTALLED_APPS 下添加 'django.contrib.admin' 并删除序列化程序解决了该问题。

我希望这也能解决其他人的问题。

PS:调试器是你最好的 friend 。 ;)

关于python - django-rest-auth:密码重置功能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47929991/

相关文章:

django - 对多个数据库使用多个 Django 管理站点

javascript - Django 中的通用进度条

django - 在 django rest 框架中过滤嵌套序列化程序

python - 克服 python 中的 opencv CV_IO_MAX_IMAGE_PIXELS 限制

python - 用于通过单一配置生成 Linux、Windows 和 MacOSX 安装程序的应用程序

python - 查询中的 PyMySQL 变量

django - 如何使 Django 的 DATETIME_FORMAT 处于事件状态?

python - Django 用对象注释查询集

django - 序列化器不显示外键数据

python - 无法 pip 安装异常包