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

时间:2018-03-03 17:11:38

标签: django url import change-password

我想使用默认的django.contrib.auth.views通过电子邮件确认重置密码。所有这些代码都在urls.py上:

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

app_name = 'houses'

urlpatterns = [

    # Root and details page
    url(r'^$', views.index, name='index'),
    url(r'^(?P<house_id>[0-9]+)/$', views.view_house, name='index'),

    # Register / login / logout
    url(r'^register/$', views.UserFormView.as_view(), name='register'),
    url(r'^login/$', views.login_user, name='login_user'),
    url(r'^logout/$', views.logout_user, name='logout_user'),

    # User profiles and edit profiles
    url(r'^profile/$', views.view_profile, name='view_profile'),
    url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),

    # ---- ERRORS ARE HERE ---- change / reset passwords 
    url(r'^change_password/$', views.change_password, name='change_password'),
    url(r'^password_reset/$', auth_views.password_reset,
        {'post_reset_redirect': 'houses:password_reset_done',}, name='password_reset'),
    url(r'^password_reset/done/$', auth_views.password_reset_done, name='password_reset_done'),
    url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
        auth_views.password_reset_confirm, name='password_reset_confirm'),

]

无论我尝试什么,我一直得到:

Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.

是因为我的应用名称是房屋吗?我一直在努力工作几个小时而没有运气。

3 个答案:

答案 0 :(得分:2)

这个错误是因为 Django 希望在项目 urls 中找到 url password_reset_complete 而不是在应用程序 urls 中,所以要在应用程序中保留该 url,您需要重写模板 password_template_email.html 并将其传递到 url 中password_reset 传递参数 email_template_name:

path('reset_password/',
 auth_views.PasswordResetView.as_view(
        template_name="users/registration/password_reset.html",
        email_template_name = 'users/registration/password_reset_email.html',
        success_url=reverse_lazy('users:password_reset_done')),
    name="reset_password"),

并在模板password_reset_email中,通过

{% autoescape off %}
To initiate the password reset process for your Account {{ user.email }},
click the link below:

{{ protocol }}://{{ domain }}{% url 'youapp:password_reset_confirm' uidb64=uid token=token %}

If clicking the link above doesn't work, please copy and paste the URL in a new browser
window instead.

{% endautoescape %}

希望能帮到你!

答案 1 :(得分:0)

如果您在模板中使用反向网址

{% url 'houses:password_reset_confirm' uidb64=<uidb64> token=<token> %}

如果你在python代码中使用反向url,那么使用

reverse('houses:password_reset_confirm', args=(<uidb64>,<token>,))

此处,&lt; uidb64&gt;表示uidb64值       &LT; token&GT;表示token

答案 2 :(得分:0)

我之前也遇到过相同的错误,要解决此问题,我只在主URL文件(project_name / urls.py)中写了reset password选项的URL,然后将其添加到模板中文件夹中有一个名为“ 注册”的文件夹,该文件夹中有一个名为password_reset_email.html的文件(其中包含将发送给用户的电子邮件,类似:

{% autoescape off %}
To initiate the password reset process for your Account {{ user.email }},
click the link below:

{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}

If clicking the link above doesn't work, please copy and paste the URL in a new browser
window instead.

{% endautoescape %}

)。如果您想清楚地查看它,可以在github上查看我的代码https://github.com/Ninou01/customer-relationship-management-system/blob/master/crm1/urls.py