如果在django中重置密码电子邮件链接错误,则找不到页面(404)错误

时间:2017-08-26 10:49:13

标签: python django forgot-password

我正在使用django默认密码重置功能。我通过电子邮件获取设置的新密码电子邮件链接。

如果网址正确,那就没关系了。现在,如果我输入错误或无效的链接,我就会得到,

The current URL, account/reset/NTQ/4ox-7f135b9b74e7a4aa909fdd/, didn't match any of these.

accounts / urls.py -

url(r'^password_reset/$', auth_views.password_reset, name='password_reset'),
url(r'^password_reset/done/$', auth_views.password_reset_done, name='password_reset_done'),
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
    auth_views.password_reset_confirm, name='password_reset_confirm'),
#url(r'^reset/',auth_views.password_reset_confirm, name='empty_password_reset_confirm'),
url(r'^reset/done/$', auth_views.password_reset_complete, name='password_reset_complete'),

模板password_reset_confirm.html -

{% extends 'base.html' %}

{% block content %}
  {% if validlink %}
    <h3>Change password</h3>
    <form method="post">
      {% csrf_token %}
      {{ form.as_p }}
      <button type="submit">Change password</button>
    </form>
  {% else %}
    <p>
      The password reset link was invalid, possibly because it has already been used.
      Please request a new password reset.
    </p>
  {% endif %}
{% endblock %}

我试过 -

url(r'^reset/',auth_views.password_reset_confirm, name='empty_password_reset_confirm')

但在这种情况下,我得到了AssertionError at /account/reset/NTQ/4ox-7f135b9b74e7a4aa909fdd/

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您必须修改网址的正则表达式,使其适用于所有网址

url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,23})/$',
    auth_views.password_reset_confirm, name='password_reset_confirm'),

对于您提到的示例链接,上述内容将起作用。您可以根据需要以相同的方式修改正则表达式以获取更多案例。

当您的网址转到带有错误令牌的链接时,它会抛出错误,因此您的其他模板部分会显示出来。