You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django 1.11中登录登出URL配置反向匹配失败问题求助

Why You're Getting the "No Reverse Match" Error for the 'login' URL

Ah, I spot the issue right away! When you migrated from Django 1.8's function-based authentication views to Django 1.11's class-based ones, you forgot to include the name parameter in your URL patterns—and that's exactly what Django uses to resolve {% url "login" %} in your templates and settings like LOGIN_URL.

The Root Cause

In your original Django 1.8 code, each URL pattern had a name attribute that acted as a unique identifier for reverse lookups:

url(r'^login/$', 'django.contrib.auth.views.login', name='login'),

But in your updated code, those critical name attributes are missing:

url(r'^login/$', auth_views.LoginView.as_view(template_name='registration/login.html')),

Without name='login', Django can't map the string "login" to any URL pattern, hence the "no reverse match" error.

The Fix

Add back the name attributes to each of your auth URL patterns, just like you had in the old code. Here's the corrected URLconf:

from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views

url(r'^login/$', auth_views.LoginView.as_view(template_name='registration/login.html'), name='login'),
url(r'^logout/$', auth_views.LogoutView.as_view(template_name='registration/logged_out.html'), name='logout'),
url(r'^logout-then-login/$', auth_views.LogoutThenLogin.as_view(template_name='logout_then_login.html'), name='logout_then_login'),

Quick Additional Checks

  • Double-check your template paths: you mentioned original templates were in template/account, but your new code uses registration/login.html. If your templates are still in account, update the template_name parameter to match (e.g., template_name='account/login.html').
  • Your settings.py uses reverse_lazy('login') for LOGIN_URL—once you add name='login' to the URL pattern, this will also work correctly.

That should resolve the reverse matching error completely!

内容的提问来源于stack exchange,提问作者Ian Waring

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:35:07