【问题标题】:NoReverseMatch at /resetpassword/ in DjangoDjango 中 /resetpassword/ 的 NoReverseMatch
【发布时间】:2015-08-22 10:14:48
【问题描述】:

我在重置密码期间收到此错误。我有一个登录页面,其中包含重置忘记密码的链接,它正确显示了模板,但是如果我写邮件发送重置链接,它会显示此错误:

本地主机/重置密码

NoReverseMatch at /resetpassword/
Reverse for 'password_reset_confirm' with arguments '()' and keyword      arguments '{'token': '42e-71994a9bf9d36c22eb95', 'uidb64': b'MQ'}' not found. 0   pattern(s) tried: []
Request Method: POST
Request URL:    http://localhost/resetpassword/
Django Version: 1.8.2
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'password_reset_confirm' with arguments '()' and keyword arguments '{'token': '42e-71994a9bf9d36c22eb95', 'uidb64': b'MQ'}' not found. 0 pattern(s) tried: []
Exception Location: C:\Python34\lib\site-   packages\django\core\urlresolvers.py in _reverse_with_prefix, line 496
Python Executable:  C:\Python34\python.exe
Python Version: 3.4.3
Python Path:    
['c:\\labsoft',
'C:\\Python34\\lib\\site-packages\\psycopg2-2.6-py3.4-win-amd64.egg',
'C:\\Windows\\SYSTEM32\\python34.zip',
'C:\\Python34\\DLLs',
'C:\\Python34\\lib',
'C:\\Python34',
'C:\\Python34\\lib\\site-packages']
 Server time:   Dom, 7 Jun 2015 21:46:55 -0500

模板渲染时出错

     In template C:\Python34\lib\site-  packages\django\contrib\admin\templates\registration\password_reset_email.html, error at line 6


1   {% load i18n %}{% autoescape off %}
2   {% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}
3   
4   {% trans "Please go to the following page and choose a new password:" %}
5   {% block reset_link %}
6   
  {{ protocol }}://{{ domain }}
  {% url 'password_reset_confirm' uidb64=uid token=token %}



7   {% endblock %}
8   {% trans "Your username, in case you've forgotten:" %} {{ user.get_username }}
9   
10  {% trans "Thanks for using our site!" %}
11  
12  {% blocktrans %}The {{ site_name }} team{% endblocktrans %}
13  
14  {% endautoescape %}
15  

这段代码用红色显示:

{{ protocol }}://{{ domain }}
{% url 'password_reset_confirm' uidb64=uid token=token %}

我认为错误可能是协议和域参数,但我不知道如何修复它。非常感谢!!!

urls.py

from django.conf.urls import patterns, url
from .views import Buscar_view

urlpatterns = patterns('melomanos.views',
                   url(r'^$','trabajos_all_view',name='url_index'),
                    url(r'^register/$','register_view',name='vista_registro'),
                   url(r'^login/$','login_view',name='vista_login'),
                   url(r'^logout/$','logout_view',name='vista_logout'),
                   url(r'^perfil/$','registro_view',name='vista_perfil'),
                   url(r'^publicar/$','trabajomusical_view', name='vista_publicar'),
                   url(r'^trabajos/$','trabajos_view',name='vista_trabajos'),
                   url(r'^trabajo/(?P<id_trabajo>.*)/$','solo_trabajo_view', name='vista_trabajo'),
                   url(r'^buscar/$',Buscar_view.as_view(),name='vista_buscar'),
                   )

 urlpatterns += patterns('',
                   url(r'^resetpassword/passwordsent/$', 'django.contrib.auth.views.password_reset_done', name='password_reset_done'),
                   url(r'^resetpassword/$', 'django.contrib.auth.views.password_reset', name="reset_password"),
                   url(r'^reset/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>,+)/$', 'django.contrib.auth.views.password_reset_confirm'),
                   url(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete',{'template_name' : 'registration/password_reset.html',  'post_reset_redirect': '/logout/' }),
                   )

【问题讨论】:

  • 分享您的 urls.py 将有助于回答。

标签: django django-templates passwords reset


【解决方案1】:

实际上,您的 token 参数的正则表达式匹配单个或多个逗号。

您可以使用&lt;token&gt;.+ 匹配任何字符。

另外,您的模式正在寻找uidb64token 之间的-,以及这一行

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

在没有- 的情况下传递两个参数。

你只需要改变 url 行如下:

url(r'^reset/(?P<uidb64>[0-9A-Za-z]+)/(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm', name='password_reset_confirm'),

【讨论】:

  • 这个解决方案在某种程度上对我有用,邮件已发送并且我已收到,但链接仍然显示错误:NoReverseMatch at /reset/MQ-42e-71994a9bf9d36c22eb95/ Reverse for 'password_reset_complete' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] ------- Request Method: GET Request URL: http://localhost/reset/MQ-42e-71994a9bf9d36c22eb95/ Django Version: 1.8.2 Exception Type: NoReverseMatch Exception Value: Reverse for 'password_reset_complete' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
  • 来自文档:docs.djangoproject.com/en/1.8/topics/auth/default/…。您完成密码重置的网址缺少名称参数,请改用:url(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete',{'template_name' : 'registration/password_reset.html', 'post_reset_redirect': '/logout/' }, name='password_reset_complete')
  • 我仍然有这个错误:TypeError at /reset/MQ-42e-71994a9bf9d36c22eb95/ password_reset_complete() got an unexpected keyword argument 'token' Request Method: GET Request URL: http://localhost/reset/MQ-42e-71994a9bf9d36c22eb95/ Django Version: 1.8.2 Exception Type: TypeError Exception Value: password_reset_complete() got an unexpected keyword argument 'token' Exception Location: C:\Python34\lib\site-packages\django\core\handlers\base.py in get_response, line 132 ------ response = Wrapped_callback(request, *callback_args, **callback_kwargs)`
  • 我有这个错误:Exception Value: password_reset_complete() got an unexpected keyword argument 'token'谢谢你的帮助!!!
  • 我们可以去聊天吗?
【解决方案2】:

password_reset_confirm 添加到网址名称

urlpatterns += patterns('',
                   url(r'^resetpassword/passwordsent/$', 'django.contrib.auth.views.password_reset_done', name='password_reset_done'),
                   url(r'^resetpassword/$', 'django.contrib.auth.views.password_reset', name="reset_password"),
                   url(r'^reset/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>,+)/$', 'django.contrib.auth.views.password_reset_confirm', name='password_reset_confirm'),
                   url(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete',{'template_name' : 'registration/password_reset.html',  'post_reset_redirect': '/logout/' }),
                   )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 2017-05-21
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多