Django密码重置'Reverse not found'为'password_reset_done'

我得到的完整错误消息是:

使用参数'()'和未找到关键字参数'{}'来反转'password_reset_done' . 尝试过0种模式:[]

我正在尝试使用标准的DJANGO auth库来实现登录,注销,密码重置和注册功能/视图到我的网站 .

我正在使用Anaconda 4.6软件包,我正在导入DJANGO库,如下所示

from django.contrib.auth import views as auth_views

我有的(相关的)urlpatterns是:

url(r'^password_reset/$', auth_views.password_reset, name='password_reset'),

url(r'^password_reset/done/$', auth_views.password_reset_done, name='password_reset_done'),

url(r'^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, name='password_reset_confirm'),

url(r'^reset/done/$', auth_views.password_reset_complete, name='password_reset_complete'),

如果我转到URL

/ password_reset / done /

我到达了这个页面 .

如果我转到URL

/ password_reset /

我得到了失败的反向错误 .

我已经阅读了2天的文档,但我似乎无法找到原因

url(r'^password_reset/done/$', auth_views.password_reset_done, name='password_reset_done'),

阻止了django反转功能 . 有没有人有任何想法?

非常感谢!

此处还有回溯以获取更多详细信息:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/password_reset/

Django Version: 1.10.5
Python Version: 3.5.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'bootstrap3',
 'app1']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "E:\Programming\Anaconda3_4.2.0\lib\site-packages\django\core\handlers\exception.py" in inner
  39.             response = get_response(request)

File "E:\Programming\Anaconda3_4.2.0\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "E:\Programming\Anaconda3_4.2.0\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "E:\Programming\Anaconda3_4.2.0\lib\site-packages\django\contrib\auth\views.py" in inner
  47.         return func(*args, **kwargs)

File "E:\Programming\Anaconda3_4.2.0\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "E:\Programming\Anaconda3_4.2.0\lib\site-packages\django\contrib\auth\views.py" in password_reset
  189.         post_reset_redirect = reverse('password_reset_done')

File "E:\Programming\Anaconda3_4.2.0\lib\site-packages\django\urls\base.py" in reverse
  91.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "E:\Programming\Anaconda3_4.2.0\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
  392.             (lookup_view_s, args, kwargs, len(patterns), patterns)

Exception Type: NoReverseMatch at /password_reset/
Exception Value: Reverse for 'password_reset_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

添加一些额外信息:

项目网址:

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^app1/',include('app1.urls')),
    url(r'^',include('app1.urls')), 
]

“app1 \ registration \ login.html”的模板代码正确呈现:

{% block title %}Login{% endblock %}

{% block content %}
  <h2>Login</h2>
  <form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Login</button>
  </form>
{% endblock %}

“app1 \ registration \ password_reset_done.html”的模板代码会抛出错误:

{% block content %}
  <p>
    We've emailed you instructions for setting your password, if an account exists with the email you entered.
    You should receive them shortly.
  </p>
  <p>
    If you don't receive an email, please make sure you've entered the address you registered with,
    and check your spam folder.
  </p>
{% endblock %}

回答(1)

2 years ago

虽然我不确定这个解决方案是否真的是最好的,但它使我能够使用自定义模板并通过在myappurls.py的urls模式中明确填写kwargs中的工作流来避免反向错误:

从django.contrib.auth导入视图作为auth_views

url(r'^login/$', auth_views.login, {'template_name': 'myapp/registration/login.html'},name='login'),
url(r'^logout/$', auth_views.logout, {'next_page': 'myapp:home'},name='logout'),

url(r'^password_reset/$', auth_views.password_reset,{'email_template_name':'myapp/registration/password_reset_email.html',
                                                    'template_name':'myapp/registration/password_reset_form.html',
                                                    'subject_template_name':'myapp/registration/password_reset_subject.txt',
                                                    'post_reset_redirect':'myapp:password_reset_done',
                                                    'from_email':'myapp@django.com',
                                                    },name='password_reset'),

url(r'^password_reset/done/$', auth_views.password_reset_done, {'template_name': 'myapp/registration/password_reset_done.html'}, name='password_reset_done'),

url(r'^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,
                                                    {'template_name': 'myapp/registration/password_reset_confirm.html',
                                                    'post_reset_redirect': 'myapp:password_reset_complete'},
                                                    name='password_reset_confirm'),

url(r'^reset/done/$', auth_views.password_reset_complete, {'template_name': 'myapp/registration/password_reset_complete.html'},name='password_reset_complete'),

更新的答案!


虽然我在第一个答案中找到了解决问题的方法,但我终于明白了真正的问题是什么 .

问题是我的项目urls.py和myapp / urls.py之间没有正确配置名称空间 . 要正确排列所有内容,我需要将项目urls.py更改为指向app / urls.py和 give it a namespace name:

url(r'^',include('myapp.urls'), name='myapp)

然后在app / urls.py中我需要为rever方法分配一个命名空间名称来连接这两个:

app_name = 'myapp'

urlpatterns = [** url(r'password_reset/done/', auth_views.password_reset_done,name='password_reset_done'), url(r'password_reset/', auth_views.password_reset, name='password_reset'), **]

现在一切都完美无缺,因为它应该没有任何桶装的hacky参数 .