Django忘记密码电子邮件系统时出现NoReverseMatch错误

时间:2018-02-11 10:47:24

标签: python django forgot-password

我正在尝试创建一个系统,通过电子邮件重置忘记的密码,但我遇到了一些错误。

我的网址是:

contactRelationUpdate

当电子邮件地址在from django.contrib.auth import views as auth_views urlpatterns = [ url(r'^$', auth_views.login, name='login'), url(r'^logout/$', auth_views.logout, name='logout'), ## more irrelevant urls here ## url(r'^password/reset/done/$', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'), url(r'^password/reset/$', auth_views.PasswordResetView.as_view(), name='password_reset'), url(r'^password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'), url(r'^password/reset/complete/$', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'), ] 处提交时,电子邮件会发送,但我收到此错误:

password_reset

当我将链接复制到浏览器并重置密码时,密码会正确重置,但会出现以下错误:

Internal Server Error: /password/reset/
Traceback (most recent call last):
  File "C:\python\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\python\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\python\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\python\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\python\lib\site-packages\django\utils\decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "C:\python\lib\site-packages\django\utils\decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\python\lib\site-packages\django\utils\decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "C:\python\lib\site-packages\django\contrib\auth\views.py", line 439, in dispatch
    return super(PasswordResetView, self).dispatch(*args, **kwargs)
  File "C:\python\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\python\lib\site-packages\django\views\generic\edit.py", line 183, in post
    return self.form_valid(form)
  File "C:\python\lib\site-packages\django\contrib\auth\views.py", line 453, in form_valid
    return super(PasswordResetView, self).form_valid(form)
  File "C:\python\lib\site-packages\django\views\generic\edit.py", line 79, in form_valid
    return HttpResponseRedirect(self.get_success_url())
  File "C:\python\lib\site-packages\django\views\generic\edit.py", line 67, in get_success_url
    if self.success_url:
  File "C:\python\lib\site-packages\django\utils\functional.py", line 114, in __wrapper__
    res = func(*self.__args, **self.__kw)
  File "C:\python\lib\site-packages\django\urls\base.py", line 91, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "C:\python\lib\site-packages\django\urls\resolvers.py", line 497, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'password_reset_done' not found. 'password_reset_done' is not a valid view function or pattern name.
[11/Feb/2018 14:32:04] "POST /password/reset/ HTTP/1.1" 500 121086

从技术上讲,系统功能正常,但显然这些错误信息并不理想。

以下是我提出的其他问题:

NoReverseMatch error with password reset emails

Error with system for email password resets

这是我跟随的tutorial

2 个答案:

答案 0 :(得分:0)

我不知道在较新的Djangos中这是否仍然存在,但在以前的版本中我们已经做到了这一点:

# django login views
urlpatterns += patterns(
    'django.contrib.auth.views',
    url(r'^logout/$',
        'logout',
        dict(
            template_name='my/logout.html',
            redirect_field_name='next',
        )),

    url(r'^password-reset/$',
        'password_reset',
        dict(
            template_name='my/password-reset-form.html',
            email_template_name='my/password-reset-email.html',
            subject_template_name='my/password-reset-email-subject.html',
            from_email=settings.FROM_EPOST,
        ),
        name="my-password-reset"),

    url(r'^password-reset-done/$',
        'password_reset_done',
        dict(
            template_name='my/password-reset-done.html',
        )),

    url(r'^password-reset-confirm/(?P<uidb64>.+)/(?P<token>.+)/$',
        'password_reset_confirm',
        dict(
            template_name='my/password-reset-confirm.html',
            # post_reset_redirect='url',
        )),

    url(r'^password-reset-complete/$',
        'password_reset_complete',
        dict(
            template_name='my/password-reset-complete.html',
        )),
)

我似乎记得在这些网址中有斜线存在问题,但是自从我写这些网址以来已经很长时间了。

电子邮件模板包含:

{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb64=uid token=token %}
{% endblock %}

答案 1 :(得分:0)

你没有反向匹配,因为password_reset函数重定向到('password_reset_done')而不是你的app命名空间 有关详细信息,您可以查看here

中的文档

例如,如果您选择app的包含网址如下:

urlpatterns = [
    url(r'election', include(election.urls, namespace="SomeName")),

]

您可以通过删除include url中的名称/命名空间来解决问题,或者需要覆盖password_reset函数。  来自django.contrib.auth.view import password_reset

#views.py
def new_password_reset(password_reset):
    if post_reset_redirect is None:
        post_reset_redirect = reverse('SomeName:password_reset_done')

    return TemplateResponse(request, template_name, context) 

#urls.py
from election import views as election_views
#and in your urls change that url:
url(r'^password_reset/$', auth_views.password_reset, name='password_reset')

#with that url :
url(r'^password_reset/$', election_views.new_password_reset, name='password_reset')

你应该没事