简体   繁体   English

使用 Django 密码重置登录时 NoReverseMatch

[英]NoReverseMatch at login with Django password reset

I have a small Django application.我有一个小的 Django 应用程序。 I am setting up authentication and I have just finished the forgotten/reset password functionality.我正在设置身份验证,并且刚刚完成忘记/重置密码功能。 However, once the password is reset it does not redirect to the success page.但是,一旦重置密码,它就不会重定向到成功页面。 I've trawled through all my code and cannot find any references to 'login' that is incorrectly coded.我浏览了我所有的代码,但找不到任何对编码错误的“登录”的引用。 The error that appears is the following: 'Reverse for 'login' not found.出现的错误如下:未找到“登录”的反向。 'login' is not a valid view function or pattern name' “登录”不是有效视图 function 或模式名称

Here's the relevant files from my project:这是我项目中的相关文件:

account urls.py帐户网址.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: 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 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 %}

I have had NoReverseMatch errors in the past and have managed to fix them, but I'm a bit stumped with this one我过去遇到过 NoReverseMatch 错误并设法修复了这些错误,但我对这个错误有点困惑

Any help would be great!任何帮助都会很棒!

Your current reset password functionality expects a url with the name login .您当前的重置密码功能需要名称为login的 url。 You've defined the login view in an app that has an app_name of "account".您已经在 app_name 为“account”的应用程序中定义了登录视图。 This creates a namespace for all of the urls in that module.这会为该模块中的所有 url 创建一个命名空间。 So your login view has a name of account:login .因此,您的登录视图的名称为account:login

Do you happen to have settings.LOGIN_URL set to "login"?您碰巧将settings.LOGIN_URL设置为“登录”吗? Something is likely calling reverse("login") or {% url "login" %} .有些东西可能正在调用reverse("login"){% url "login" %} Changing that to "account:login" will fix it or moving your login view up to the root out of the account namespace will work.将其更改为"account:login"将修复它,或者将您的登录视图向上移动到帐户命名空间之外的根目录将起作用。

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

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