【问题标题】:Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name未找到“password_reset_confirm”的反向。 'password_reset_confirm' 不是有效的视图函数或模式名称
【发布时间】:2018-03-03 17:11:38
【问题描述】:

我想使用默认的 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: password_reset_confirm' 。像':'

标签: django url import change-password


【解决方案1】:

这个错误是因为 Django 期望在项目 urls 中而不是在应用程序 urls 中找到 url password_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 %}

希望我能帮上忙!

【讨论】:

    【解决方案2】:

    如果您在模板中使用反向 url

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

    如果你在 python 代码中使用反向 url 然后使用

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

    这里,uidb64> 表示uidb64 值 token> 表示token

    【讨论】:

      【解决方案3】:

      我之前遇到过同样的错误,为了解决这个问题,我只是在主 URL 文件 (project_name/urls.py) 中编写了重置密码选项的 URL,并添加了模板文件夹 一个名为“registration”的文件夹,其中包含一个名为 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-04
        • 2021-05-30
        • 2019-03-17
        • 2018-11-19
        • 2019-04-06
        • 2019-08-13
        • 2019-11-04
        • 1970-01-01
        相关资源
        最近更新 更多