Django - NoReverseMatch在/ accounts / password_reset /

时间:2016-02-17 00:42:50

标签: python django

我正在尝试使用内置的Django密码重置。通过Django docs查看,根据我收集的内容,它基本上只需添加一些网址urls.py

urls.py

urlpatterns = [
    url(r'^login/$', views.login_view, name='login'),
    url(r'^password_reset/$', auth_views.password_reset, {'post_reset_redirect' : '/accounts/password_reset_done/'}, name='password_reset'),
    url(r'^password_reset_done/$', auth_views.password_reset_done, name='password_reset_done'),
    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.password_reset_confirm, {'post_reset_redirect' : '/accounts/password_done/'}, name='password_reset_confirm'),
    url(r'^password_done/$', auth_views.password_reset_complete, name='password_done'),
    url(r'^auth/$', views.login_auth, name='login_auth'),
    url(r'^register/$', views.register_user, name='register'),
    url(r'^logout/$', views.logout_user, name='logout'),
]

login.html

{% extends "base.html" %}

{% block content %}

    <title>{% block title %} | Login{% endblock %}</title>

    <form action="/accounts/auth/" method="POST">
        <h2>Login</h2>
        {% if error_message %}<div class="formfielderrors">* {{ error_message }}</div>{% endif %}
        {% csrf_token %}
        <label for="username">Username: </label>
        <br>
        <input type="text" name="username" value="" id="username">
        <br><br>
        <label for="password">Password: </label>
        <br>
        <input type="password" name="password" value="" id="password">
        <br><br>
        <a href="/accounts/password_reset/">Forgot Password?</a>
        <br><br>
        <input type="submit" value="Login" class="greenButton">
    </form>

{% endblock content %}

我很确定我已正确添加这些网址,并且r'^password_reset/$'页面正确加载,但在输入我的电子邮件并点击“重置我的密码”后,我收到以下错误:

  

/ accounts / password_reset /

的NoReverseMatch

我不完全确定这个错误甚至意味着什么,所以我很难调试。从查看文档来看,这似乎不是很难实现的功能,但我似乎无法让它工作。我在这里做错了什么?

完整错误:

response = self.process_exception_by_middleware(e, request)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/contrib/auth/views.py", line 49, in inner
    return func(*args, **kwargs)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/contrib/auth/views.py", line 205, in password_reset
    form.save(**opts)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/contrib/auth/forms.py", line 259, in save
    html_email_template_name=html_email_template_name)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/contrib/auth/forms.py", line 208, in send_mail
    body = loader.render_to_string(email_template_name, context)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/loader.py", line 97, in render_to_string
    return template.render(context, request)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/backends/django.py", line 95, in render
    return self.template.render(context)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/base.py", line 206, in render
    return self._render(context)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/base.py", line 197, in _render
    return self.nodelist.render(context)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/base.py", line 988, in render
    bit = node.render_annotated(context)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/base.py", line 955, in render_annotated
    return self.render(context)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/defaulttags.py", line 41, in render
    output = self.nodelist.render(context)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/base.py", line 988, in render
    bit = node.render_annotated(context)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/base.py", line 955, in render_annotated
    return self.render(context)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/loader_tags.py", line 60, in render
    result = self.nodelist.render(context)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/base.py", line 988, in render
    bit = node.render_annotated(context)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/base.py", line 955, in render_annotated
    return self.render(context)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/defaulttags.py", line 513, in render
    six.reraise(*exc_info)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/template/defaulttags.py", line 499, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 600, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 508, in _reverse_with_prefix
    (lookup_view_s, args, kwargs, len(patterns), patterns))
NoReverseMatch: Reverse for 'password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb64': 'MQ', u'token': u'49h-b07b0dfeb4aa82d09149'}' not found. 0 pattern(s) tried: []
[17/Feb/2016 01:47:33] "POST /accounts/password_reset/ HTTP/1.1" 500 173812

1 个答案:

答案 0 :(得分:4)

问题来自于您使用帐户网址的命名空间和视图中使用的Django内置模板。在模板registration/password_reset_email.html中,您会看到:

{% url 'password_reset_confirm' uidb64=uid token=token %}

但是,您的应用中不存在网址名称password_reset_confirm,因为您使用了名称空间“帐户” - 网址改为accounts:password_reset_confirm

您可以考虑创建一个单独的文件来存储不使用“accounts”命名空间的网址。 E.g。

# urls_django_auth.py
url_patterns = [
    url(r'^password_reset/$', auth_views.password_reset, ...),
    url(r'^password_reset_done/$', auth_views.password_reset_done, ...),
    ... other django auth views ...
]

# urls.py
url_patterns = [
    ....
    url(r'^accounts/', include('accounts.urls', namespace='accounts')),
    url(r'^accounts/', include('accounts.urls_django_auth')),  
]

或者您可以使用正确的网址格式创建自己的password_reset_email.html版本,并将其传递给您的视图

url(r'^password_reset/$', auth_views.password_reset, {
    'post_reset_redirect' : '/accounts/password_reset_done/',
    'email_template_name': '/your/own/password_reset_email.html'     
}, name='password_reset')