django - 未找到 'password_reset_confirm' 的反向。 'password_reset_confirm' 不是有效的 View 函数或模式名称

标签 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.

是因为我的应用名称是houses吗?我已经尝试了几个小时但没有运气。

最佳答案

此错误是因为 Django 希望在项目 url 中而不是在应用程序 url 中找到 urlpassword_reset_complete,因此为了在应用程序中保留该 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 %}

希望对您有帮助!

关于django - 未找到 'password_reset_confirm' 的反向。 'password_reset_confirm' 不是有效的 View 函数或模式名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49086882/

相关文章:

python - Django:如何获取查询集的相关对象?

node.js - 如何统计Nodejs中的URL访问量,express?

url - Base64 编码的 uuid 比预期的要长

reactjs - 为什么需要在父子组件中多次导入React?

hadoop - 在具有困惑数据且未修改时间列的表上以sqoop增量导入

django - 如何强制 select_related() 选择具有 null=True 属性的外键?

python - 如何在 Django 或 Flask 中嵌入交互式 Bokeh 或 Dash 应用*带身份验证*?

python - 选择相关 - 从外键中选择一个对象

url - 同一网址的不同映射

python - 删除某个版本的 python 包