简体   繁体   中英

NoReverseMatch at login with Django password reset

I have a small Django application. 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'

Here's the relevant files from my project:

account urls.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 %}

I have had NoReverseMatch errors in the past and have managed to fix them, but I'm a bit stumped with this one

Any help would be great!

Your current reset password functionality expects a url with the name login . You've defined the login view in an app that has an app_name of "account". This creates a namespace for all of the urls in that module. So your login view has a name of account:login .

Do you happen to have settings.LOGIN_URL set to "login"? Something is likely calling reverse("login") or {% 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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