错误反向' password_reset_done'未找到。 ' password_reset_done'不是有效的视图函数或模式名称。

时间:2017-06-21 12:59:29

标签: django-views

我一直收到这个错误 反向' password_reset_done'未找到。 ' password_reset_done'不是有效的视图函数或模式名称。 我正在尝试使用默认视图 来自django.contrib.auth.views

from django.conf.urls import url 
from accounts import views 
from django.contrib.auth.views import (login, 
logout, 
password_reset, 
password_reset_done, 
password_reset_confirm, 
) 


urlpatterns =[ 
url(r'^$', views.cover, name='cover'), 
url(r'^home/$', views.home, name = 'home'), 
url(r'^login/$', login, {'template_name':'accounts/login.html'}, name ="login"), 
url(r'^logout/$', logout, {'template_name':'accounts/logout.html'}, name = "logout"), # views define a link to connecct this to views then to template 
url(r'^register/$', views.register, name="register"), 
url(r'^profile/$', views.view_profile, name='view_profile'), 
url(r'^profile/edit/$', views.edit_profile, name='edit_profile'), 
url(r'^change-password/$', views.change_password, name='change_password'), 
url(r'^reset-password/$', password_reset, name= 'password_reset'), 
url(r'^reset-password/done/$', password_reset_done, name='password_reset_done'), 
url(r'^reset-password/confirm/$', password_reset_confirm, name='password_reset_confirm') 
]

请有人帮助我...我已经检查了所有......但是找不到错误。

8 个答案:

答案 0 :(得分:3)

试试这个 小心注意一些错误,比如不在某些网址末尾添加$或者添加它。还要注意给出success_url的位置,因为内部代码使用它并且在没有它的情况下丢失。

在此代码中,我选择管理用户的应用程序称为帐户,您可以将其称为任何内容。

模板应该放在django识别的目录中,否则它不会在这里找到它们是settings.py的代码

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

这是完整的urls.py

from django.conf.urls import url
from django.contrib.auth import views as auth_views
from accounts.views import signup

urlpatterns = [
    url(r'signup/$', signup, name='signup'),
    url(r'login/$',auth_views.LoginView.as_view(template_name='login.html')),
    url(r'logout/$',auth_views.LogoutView.as_view(template_name='logout.html')),
    url(r'password_change/$',auth_views.PasswordChangeView.as_view(template_name='password_change.html',success_url='/accounts/password_change_done')),
    url(r'password_change_done/',auth_views.PasswordChangeDoneView.as_view(template_name='password_change_done.html')),
    url(r'password_reset/$',auth_views.PasswordResetView.as_view(template_name='password_reset.html',email_template_name='password_reset_email.html',subject_template_name='password_reset_subject.txt',success_url='/accounts/password_reset_done/',from_email='support@yoursite.ma')),
    url(r'password_reset_done/',auth_views.PasswordResetDoneView.as_view(template_name='password_reset_done.html')),
    url(r'password_reset_confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',auth_views.PasswordResetConfirmView.as_view(template_name='password_reset_confirm.html',success_url='/accounts/password_reset_complete/')),
    url(r'password_reset_complete/',auth_views.PasswordResetCompleteView.as_view(template_name='password_reset_complete.html')),
]

答案 1 :(得分:1)

如果您只在主path('', include('django.contrib.auth.urls'))中使用urls.py而不是app_name中的def addChar(_textBox, char): global charCount if charCount <= 15: if char == "backSpace": _textBox.delete(charCount, END) if charCount != 0: charCount = charCount - 1 else: _textBox.insert(END, char) charCount = charCount + 1 print(charCount) ,那么它将会正常工作。

答案 2 :(得分:1)

当Django从1.xx更新到2.xx时,就会发生这种情况(就像我自己的情况一样)。

这是我的解决方案。我通过添加包含键“ post_change_redirect ”的字典来修复它,该字典的值指向password_change_done网址。要重置密码,请使用post_rest_redirect

from django.contrib.auth import views as v
from django.conf.urls import url

urlpatterns = 
......
    url(r"password-change/$", v.password_change, {"post_change_redirect":"account:password_change_done"}, name="password_change")
.........

]

答案 3 :(得分:0)

我找到了这段代码,这似乎有效。

from django.conf.urls import url
from accounts import views
from django.contrib.auth.views import  (login, 
                                    logout,     
                                    password_reset, 
                                    password_reset_done,
                                    password_reset_confirm, 
                                )
from django.contrib.auth import views as auth_views


urlpatterns =[
    url(r'^$', views.cover, name='cover'), 
    url(r'^home/$', views.home, name = 'home'),
    url(r'^login/$', login, {'template_name':'accounts/login.html'}, name ="login"), 
    url(r'^logout/$', logout, {'template_name':'accounts/logout.html'}, name = "logout"),
    url(r'^register/$', views.register, name="register"),
    url(r'^profile/$', views.view_profile, name='view_profile'),
    url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),
    url(r'^change-password/$', views.change_password, name='change_password'),
    url(r'^password_reset/$', auth_views.password_reset,{'email_template_name':'accounts/registration/password_reset_email.html',
                                                    'subject_template_name':'accounts/registration/password_reset_subject.txt',
                                                    'post_reset_redirect':'accounts:password_reset_done',
                                                    'from_email':'accounts@django.com',
                                                    },name='password_reset'),

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

在myapp名称中的上述代码帐户中。你可以在哪里放置自己的应用程序名称 这是为Django 1.11

答案 4 :(得分:0)

当Django从1.xx版本升级到2.xx版本时,也可能发生此特定错误

要解决此问题:

  1. main url.py中添加此行:

    url(r'^', include('django.contrib.auth.urls'))
    
  2. ,然后将功能替换为“ your_app / urls.py”中的类。例如。

    password_reset => PasswordResetView.as_view()
    

    例如:

    url(r'^reset-password/$', PasswordResetView.as_view(), name='password_reset'),
    url(r'^reset-password/done/$', PasswordResetDoneView.as_view(), name='password_reset_done'),
    
    url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    
    url(r'^reset-password/complete/$', PasswordResetCompleteView.as_view(), name='password_reset_complete'),
    

答案 5 :(得分:0)

您缺少导入,app_name,一些模板和模板路由。

为此更改代码:

from django.conf.urls import url 
from accounts import views 
from django.contrib.auth import views as auth_views 

app_name = 'accounts' # Django 2.0+, if not add namespace = 'accounts' on the urls.py where you are including this set of urls.

urlpatterns =[ 
url(r'^$', views.cover, name='cover'), 
url(r'^home/$', views.home, name = 'home'), 
url(r'^login/$', login, {'template_name':'accounts/login.html'}, name ="login"), 
url(r'^logout/$', logout, {'template_name':'accounts/logout.html'}, name = "logout"),
url(r'^register/$', views.register, name="register"), 
url(r'^profile/$', views.view_profile, name='view_profile'), 
url(r'^profile/edit/$', views.edit_profile, name='edit_profile'), 
url(r'^change-password/$', views.change_password, name='change_password'), 
url(r'^password_reset/$', auth_views.password_reset,{'email_template_name':'registration/password_reset_email.html',
                                                    'subject_template_name':'registration/password_reset_subject.txt',
                                                    'post_reset_redirect':'accounts:password_reset_done',
                                                    'from_email':'accounts@django.com', # Yours
                                                    },name='password_reset'),

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

确保在“帐户”文件夹中拥有:/templates/registration/*.html及其所有模板

答案 6 :(得分:0)

对我来说,缺少的关键信息是需要在URL条目上设置name属性的反向解析!

    path(
    'password_change/done/',
    auth_views.PasswordChangeDoneView.as_view(template_name='SyllabusTrackerApp/change-password-done.html'),
    name="password_change_done"
),

app_name命名空间也未设置,但为简单起见,在我的设置中未使用那些命名空间,所以我可以忽略它。

答案 7 :(得分:0)

我遇到了同样的问题,但是通过在PasswordResetView中添加了success_url参数来解决了这个问题:

添加基于类的视图PasswordResetView success_url 参数。这将替换password_reset_done的默认路由

url(r'^reset/$',PasswordResetView.as_view(
    template_name='password_reset.html',
    email_template_name='password_reset_email.html',
    subject_template_name='password_reset_subject.txt',
    ...
    success_url = reverse_lazy('accounts:password_reset_done')
    ...
    ...
),name='password_reset'),