繁体   English   中英

使用 Django 密码重置登录时 NoReverseMatch

[英]NoReverseMatch at login with Django password reset

我有一个小的 Django 应用程序。 我正在设置身份验证,并且刚刚完成忘记/重置密码功能。 但是,一旦重置密码,它就不会重定向到成功页面。 我浏览了我所有的代码,但找不到任何对编码错误的“登录”的引用。 出现的错误如下:未找到“登录”的反向。 “登录”不是有效视图 function 或模式名称

这是我项目中的相关文件:

帐户网址.py

from django.urls import path, include, reverse_lazy
from django.contrib.auth import views as auth_views
from . import views

app_name = 'account'

urlpatterns = [
    #Login URLs
    path('login/', auth_views.LoginView.as_view(), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    #User Pages URLs
    path('dashboard/', views.dashboard, name='dashboard'),
    path('nodes/', include('nodes.urls')),
    #Password Changes URLs
    path('password_change/', auth_views.PasswordChangeView.as_view(success_url=reverse_lazy('account:password_change_done')), name='password_change'),
    path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(), name='password_change_done'),
    #Password Reset URLs
    path('password_reset/',
            auth_views.PasswordResetView.as_view(success_url=reverse_lazy('account:password_reset_done')),
            name='password_reset'),
    path('password_reset/done/',
            auth_views.PasswordResetDoneView.as_view(),
            name='password_reset_done'),
    path('reset/<uidb64>/<token>/',
            auth_views.PasswordResetConfirmView.as_view(success_url=reverse_lazy('account:password_reset_complete')),
            name='password_reset_confirm'),
    path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]

password_reset_confirm.html:

{% block content %}
    <div class="page-title">
        <h1>Reset Password</h1>
    </div>
    <br>
    <div class="login-container">
        <div class="login-box">
            <br>
            <h3 style="text-align: center;">Reset your password</h3>
            <div class="login-form" style="text-align: center; ">
                {% if validlink %}
                    <p>Please enter your new password twice:</p>
                    <form method="post">
                        {{ form.as_p }}
                        {% csrf_token %}
                        <p><input type="submit" value="Change my password"/></p>
                    </form>
                {% else %}
                    <p>The password reset link was invalid, possibly because it has
                        already been used. Please request a new password reset link</p>
                {% endif %}
                </form>
            </div>
        </div>
    </div>

{% endblock %}

password_reset_done.html

{% block content %}
    <div class="page-title">
        <h1>Password Reset Confirmation</h1>
    </div>
    <br>
    <div class="login-container">
        <div class="login-box">
            <br>
            <h4 style="text-align: center;">An email has been sent to the email associated with your account, follow the link
            in the email to reset your password.</h4>
            <h4>If you do not receive an email, please try again and make sure you have entered the correct email</h4>
            </div>
        </div>
    </div>

{% endblock %}

我过去遇到过 NoReverseMatch 错误并设法修复了这些错误,但我对这个错误有点困惑

任何帮助都会很棒!

您当前的重置密码功能需要名称为login的 url。 您已经在 app_name 为“account”的应用程序中定义了登录视图。 这会为该模块中的所有 url 创建一个命名空间。 因此,您的登录视图的名称为account:login

您碰巧将settings.LOGIN_URL设置为“登录”吗? 有些东西可能正在调用reverse("login"){% url "login" %} 将其更改为"account:login"将修复它,或者将您的登录视图向上移动到帐户命名空间之外的根目录将起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM