我在django 1.6网站上遇到密码重置问题。 urls.py有:
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$',
'django.contrib.auth.views.password_reset_confirm',
name='password_reset_confirm'
当我点击Reset my password
链接时,我得到:
NoReverseMatch at /user/password/reset/
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb36': 'Mw', u'token': u'4bs-b5728359cb279d542120'}' not found. 1 pattern(s) tried: ['reset/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>.+)/$']
这是追溯:
Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/views.py" in password_reset
158. form.save(**opts)
File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/forms.py" in save
256. email = loader.render_to_string(email_template_name, c)
File "/usr/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
164. return t.render(Context(dictionary))
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render
140. return self._render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/site-packages/django/template/base.py" in render_node
854. return node.render(context)
File "/usr/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
447. six.reraise(*exc_info)
File "/usr/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
433. url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
536. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
456. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /user/password/reset/
Exception Value: Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb36': 'Mw', u'token': u'4bs-b5728359cb279d542120'}' not found. 1 pattern(s) tried: ['reset/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>.+)/$']
答案 0 :(得分:2)
您需要更新正则表达式以包含_\-
,并将名称uidb36
更改为uidb64
。它应该是:
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$',
'django.contrib.auth.views.password_reset_confirm',
name='password_reset_confirm'
),
您需要更新registration/password_reset_email.html
电子邮件模板,以使用uidb64
代替uidb36
。示例电子邮件模板given in the docs是:
Someone asked for password reset for email {{ email }}. Follow the link below:
{{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
Django 1.6 release notes已涵盖这一点。