Django密码重置为'password_reset_done'

时间:2017-05-05 22:57:11

标签: django python-3.x change-password

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

  

使用参数'()'和关键字反转'password_reset_done'   没有找到参数“{}”。尝试了0种模式:[]

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

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

from django.contrib.auth import views as auth_views

我所拥有的(相关)网址是:

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

如果我转到网址

  

/ password_reset /完成/

我到达页面。

如果我转到网址

  

/ 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 个答案:

答案 0 :(得分:2)

虽然我不确定这个解决方案是否真的是最好的,但它使我能够使用自定义模板并通过在myappurls.py:

  

from django.contrib.auth import views as 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和 为其指定名称空间名称:

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参数。