【问题标题】:Django error: Reverse for 'password_reset_confirm' with no arguments not foundDjango 错误:“password_reset_confirm”的反向操作,未找到任何参数
【发布时间】:2018-05-02 15:41:54
【问题描述】:

我正在使用 Django 1.11 构建用户帐户应用程序。我的帐户应用程序的网址如下代码 1。而且我还有一个模板/注册文件夹和几个模板文件: enter image description here

在我输入电子邮件地址后,我收到了包含以下链接的电子邮件: http://127.0.0.1:8000/account/password-reset/confirm/MQ/4ra-66d3672f1d340589fbf9/

我点击上面的链接,浏览器会重定向到这个链接: http://127.0.0.1:8000/account/password-reset/confirm/MQ/set-password/

以及错误提示:

NoReverseMatch 在 /account/password-reset/confirm/MQ/set-password/

没有找到没有参数的“password_reset_confirm”反向。尝试了 1 种模式:['account/password-reset/confirm/(?P[-\w]+)/(?P[-\w]+)/$'] 请求方法:GET 请求网址:http://127.0.0.1:8000/account/password-reset/confirm/MQ/set-password/ Django 版本:1.11.7 异常类型:NoReverseMatch 异常值:
未找到任何参数的“password_reset_confirm”反向。尝试了 1 种模式:['account/password-reset/confirm/(?P[-\w]+)/(?P[-\w]+)/$']

请帮我解决这个问题。似乎我点击链接后,Django 无法呈现模板/注册文件夹下的 password_reset_confirm.html。

代码 1:

    # restore password urls
    url(r'^password-reset/$', auth_views.PasswordResetView.as_view(), name='password_reset'),
    url(r'^password-reset/done/$', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
    url(r'^password-reset/confirm/(?P<uidb64>[-\w]+)/(?P<token>[-\w]+)/$',
        auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    url(r'^password-reset/complete/$',
        auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),

【问题讨论】:

    标签: django


    【解决方案1】:

    Django 的错误消息是说您的代码已尝试将 password_reset_confirm 反转为其 url,但您尚未提供 url 模式所需的 uid64token 参数。您应该找到执行reverse() 的代码部分并更新它以提供参数:

    reverse('password_reset_confirm',args=(uid64, token))
    

    【讨论】:

    • 我已经为 url 设置了名称“password_reset_confirm”,Django 可以将名称反转为 URL。
    【解决方案2】:

    转到密码重置确认模板文件并像这样摆脱表单上的操作

    <form   method="post"> </form> 
    

    而不是这个:

    <form   action={% url 'your template file name'%} method="post"></form>
    

    【讨论】:

      【解决方案3】:

      urls.py:

      path('accounts/reset_password_confirm/<uidb64>/<token>/', PasswordResetConfirmView.as_view(), name='reset_password_confirm'),
      

      如上更改 URL 对我有用。虽然,我仍然没有收到电子邮件。错误消失了。

      【讨论】:

      • 这对我有帮助,但我需要在主 urls.py 中添加此路由才能正常工作,而不是帐户应用程序
      【解决方案4】:
      path('users/password_reset/', PasswordResetView.as_view(
          template_name='commons/password_reset_form.html',
          subject_template_name='commons/password_reset_subject.txt',
          email_template_name='commons/password_reset_email.html',
          success_url='done',), name="password_reset"),
      
      path('users/password_reset/done/', 
          PasswordResetDoneView.as_view(
          template_name='commons/password_reset_done.html'),name="password_reset_done"),
      
      path('users/reset/<uidb64>/<token>/', PasswordResetConfirmView.as_view(
          template_name='commons/password_reset_confirm.html',
          success_url='/users/reset/done/'),name="password_reset_confirm"),
      
      path('users/reset/done/',
          PasswordResetCompleteView.as_view(template_name='commons/password_reset_complete.html'),
          name="password_reset_complete"),
      

      【讨论】:

        猜你喜欢
        • 2020-01-02
        • 1970-01-01
        • 2021-04-13
        • 1970-01-01
        • 2021-05-30
        • 2021-03-13
        • 2020-05-11
        • 1970-01-01
        • 2021-07-20
        相关资源
        最近更新 更多