簡體   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