gpt4 book ai didi

python - Django - NoReverseMatch 位于/accounts/password_reset/

转载 作者:太空狗 更新时间:2023-10-30 02:57:58 25 4
gpt4 key购买 nike

我正在尝试使用内置的 Django 密码重置。翻看Django docs ,据我所知,它基本上只涉及向 urls.py

添加一些 URL

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 %}

我相当确定我已经正确添加了这些 URL,并且 r'^password_reset/$' 页面正确加载,但是在输入我的电子邮件并单击“重置我的密码”之后,我收到以下错误:

NoReverseMatch at /accounts/password_reset/

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

完整错误:

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

最佳答案

问题来自您对帐户 url 使用 namespace 和 View 中使用的 Django 内置模板。在模板 registration/password_reset_email.html 中,您将看到:

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

但是,您的应用中不存在 url 名称 password_reset_confirm,因为您使用了命名空间“accounts”- url 变成了 accounts:password_reset_confirm

您可以考虑创建一个单独的文件来存储不使用“accounts”命名空间的 url。例如

# 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')),
]

或者您可以使用正确的 URL 模式创建您自己的 password_reset_email.html 版本,并将其传递给您的 View

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')

关于python - Django - NoReverseMatch 位于/accounts/password_reset/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35445845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com