NoReverseMatch:在 Django 注册中的密码重置表单中?

Posted

技术标签:

【中文标题】NoReverseMatch:在 Django 注册中的密码重置表单中?【英文标题】:NoReverseMatch: in the password reset form in Django-registration? 【发布时间】:2014-04-29 13:36:54 【问题描述】:

访问此 URL http://127.0.0.1:8000/accounts/password/reset/ 时出现以下错误

Request Method: GET
Request URL:    http://127.0.0.1:8000/accounts/password/reset/
Django Version: 1.6.2
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'password_reset_done' with arguments '()' and keyword arguments '' not found. 0 pattern(s) tried: []

我的password_reset_form.html

% extends "registration/base.html" %
% load i18n %

% block breadcrumbs %<div class="breadcrumbs"></div>% endblock %

% block title %% trans "Password reset" %% endblock %

% block content %

<h1>% trans "Password reset" %</h1>

<p>% trans "Forgotten your password? Enter your e-mail address below, and we'll e-mail instructions for setting a new one." %</p>

<form action="" method="post">
    % csrf_token %
 form.email.errors 
<p><label for="id_email">% trans 'E-mail address:' %</label>  form.email  <input type="submit" value="% trans 'Reset my password' %" /></p>
</form>

% endblock %

编辑: 我已经按照frnhr的建议更正了,但是弹出如下错误。

TemplateSyntaxError at /accounts/password/reset/
Could not parse the remainder: ',' from 'uid,'
Request Method: POST
Request URL:    http://127.0.0.1:8000/accounts/password/reset/
Django Version: 1.6.2
Exception Type: TemplateSyntaxError
Exception Value:    
Could not parse the remainder: ',' from 'uid,'
Exception Location: /Users/sridhar/Documents/virtualenvs/django/django/lib/python2.7/site-packages/django/template/base.py in __init__, line 577
Python Executable:  /Users/sridhar/Documents/virtualenvs/django/django/bin/python
Python Version: 2.7.5

【问题讨论】:

【参考方案1】:

Django-registration 尚未与 Djagno 1.6 兼容。此补丁将解决您的问题:https://bitbucket.org/ubernostrum/django-registration/pull-request/63/django-16-compatibility-fix-auth-views/diff

注册/auth_urls.py

urlpatterns = patterns('',
                       url(r'^login/$',
                           auth_views.login,
                           'template_name': 'registration/login.html',
                           name='auth_login'),
                       url(r'^logout/$',
                           auth_views.logout,
                           'template_name': 'registration/logout.html',
                           name='auth_logout'),
                       url(r'^password/change/$',
                           auth_views.password_change,
                           'post_change_redirect': reverse_lazy('auth_password_change_done'),
                           name='auth_password_change'),
                       url(r'^password/change/done/$',
                           auth_views.password_change_done,
                           name='auth_password_change_done'),
                       url(r'^password/reset/$',
                           auth_views.password_reset,
                           'post_reset_redirect': reverse_lazy('auth_password_reset_done'),
                           name='auth_password_reset'),
                       url(r'^password/reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$',
                           auth_views.password_reset_confirm,
                           name='auth_password_reset_confirm'),
                       url(r'^password/reset/complete/$',
                           auth_views.password_reset_complete,
                           'post_reset_redirect': reverse_lazy('auth_password_reset_complete'),
                           name='auth_password_reset_complete'),
                       url(r'^password/reset/done/$',
                           auth_views.password_reset_done,
                           name='auth_password_reset_done'),
)

【讨论】:

感谢您的回答。我按照您的建议进行了更正,但是我收到了上面在编辑中粘贴的错误。请帮忙 @eagertoLearn 这看起来像是您的模板文件中的错误,希望与问题无关。在模板文件中搜索“uid”字符串。有匹配的吗? 我在上面粘贴了我的模板文件。我在那里没有看到任何“uid”字符串? @eagertoLearn 在任何其他模板中,可能在一个当前模板的父级中,或类似的东西中。在你的位置,我会对整个项目进行 grep。 “uid”一定会在某处弹出。注意逗号。 我在其他帖子上也有一个问题,您在 django-registration 中使用自定义用户模型时给出了答案。我在 bitbucket 中使用了您的提交。 http://***.com/questions/22565964/is-it-possible-to-add-fields-to-exisiting-django-user-models。请帮忙。【参考方案2】:

您得到的错误可能是因为您可能没有在 urls.py 中设置模板的路径。因此,Django 尝试使用默认注册模板,如果您没有启用 admin,您将无法使用该模板。 所以这两种解决方案, 如果你想使用 Django 的模板来重置密码(或@frnhr 上面提到的任何其他功能,请将此行添加到你的 urls.py :

url(r'^admin/', include(admin.site.urls))

或者,如果您想使用自己的模板,请将此参数添加到上面答案中的字典中

'template_name' : 'registration/password_reset_form.html' 

这会让它看起来像,

url(r'^accounts/password/reset/$',
   auth_views.password_reset,
   'post_reset_redirect': reverse_lazy('auth_password_reset_done'), 
   'template_name': 'registration/password_reset_form.html',
   name='auth_password_reset')

希望有帮助!

【讨论】:

以上是关于NoReverseMatch:在 Django 注册中的密码重置表单中?的主要内容,如果未能解决你的问题,请参考以下文章

NoReverseMatch 在渲染 Django 管理屏幕时?

没有反向匹配 - django.urls.exceptions.NoReverseMatch

NoReverseMatch:在 Django 注册中的密码重置表单中?

django 偶尔会抛出一个 NoReverseMatch

django 生产服务器中的 NoReverseMatch

Django 查看错误 - NoReverseMatch