【问题标题】:django-rest-auth: Issue with Password Reset functionaliitydjango-rest-auth:密码重置功能问题
【发布时间】:2018-06-04 10:16:02
【问题描述】:

我一直在尝试使用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: Reverse for 'account_reset_password_from_key' 未找到。'account_reset_password_from_key' 不是有效的视图函数或模式名称。”时间>。并且还没有找到任何解决方案或解决方法。

任何帮助将不胜感激。

【问题讨论】:

  • 您是否在django-rest-auth.readthedocs.io/en/latest/… 上添加了步骤 3 中所述的 URL?如果是这样,您是否命名了 URL?
  • 是的!我已经添加了提到的 URL。密码重置流程对我来说似乎很复杂。但是,经过大量代码和调试后,我发现了问题,现在它可以工作了。不过感谢您的建议!
  • 使用上面的 serializer.py 和 settings.py,我收到此错误:/rest_auth/utils.py",第 10 行,在 import_callable 包中,attr = path_or_callable.rsplit('.', 1 ) ValueError: not enough values to unpack (expected 2, got 1) 。知道我做错了什么吗?

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


【解决方案1】:

所以,我终于得到了密码重置功能。事情是这样的——

我们的 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/ 错误。经过大量调试后,我发现模板出现了问题 - registration/password_reset_email.html,它位于 Django Admin 的模板目录下。这是因为我使用的另一个 Django 应用程序禁用了 django 管理应用程序。

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

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

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

【讨论】:

    猜你喜欢
    • 2019-05-25
    • 1970-01-01
    • 2021-11-02
    • 2020-05-21
    • 2017-04-07
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 2013-09-21
    相关资源
    最近更新 更多