Django 中 /resetpassword/ 的 NoReverseMatch

NoReverseMatch at /resetpassword/ in Django

我在重置密码期间遇到此错误。我有一个登录页面 link 用于重置忘记的密码,它正确显示了模板,但如果我写邮件发送重置 link,它会显示此错误:

localhost/resesetpassword

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/' }),
                   )

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

您可以使用 <token>.+ 来匹配任何字符。

此外,您的模式正在寻找 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'),

password_reset_confirm 添加到 url 名称

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/' }),
                   )