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

Django 2认证模块的URL配置问题及代码求助

Fixing Django 2 Authentication & URL Configuration Issues in Your Music App

Let's walk through the problems in your music/urls.py and fix them one by one:

1. Resolve HTML Entity Escaping in Path Parameters

The most critical issue here is that you're using HTML escaped characters (&lt; and &gt;) instead of literal angle brackets (< and >) for path converters. Django can't parse &lt;int:pk&gt; as a valid path parameter—this will lead to 404 errors when trying to access the detail view.

Corrected URL Patterns:

from django.urls import path, re_path
from . import views

app_name = 'music'
urlpatterns = [
    # Authentication routes
    path('register/', views.register, name='register'),
    path('login_user/', views.login_user, name='login_user'),
    path('logout_user/', views.logout_user, name='logout_user'),
    
    # Fixed detail view path (replaced &lt;/&gt; with <>)
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
]

2. Verify Project-Level URL Inclusion

Make sure your project's root urls.py properly includes the music app's URLs with namespace support (since you set app_name = 'music'):

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    # Include music app URLs to enable namespace-based URL reversing
    path('music/', include('music.urls')),
]

Without this setup, reversing URLs using the namespace (like {% url 'music:login_user' %} in templates) will throw errors.

3. Validate View Definitions

Double-check that your views.py has the corresponding views correctly implemented:

  • For register, login_user, logout_user: Ensure these function-based views handle core authentication logic (e.g., login_user should validate user credentials, call auth.login(), and redirect on successful login).
  • For DetailView: Confirm it's a class-based view inheriting from django.views.generic.DetailView, with a model attribute set (e.g., model = Album) so Django knows which database model to fetch using the pk parameter.

Example of a basic valid DetailView:

from django.views.generic import DetailView
from .models import Album

class DetailView(DetailView):
    model = Album
    template_name = 'music/detail.html'

4. Optional: Simplify with Django's Built-In Auth Views

If you don't need fully custom authentication logic, Django 2.x provides ready-to-use auth views that eliminate boilerplate code. For example:

from django.contrib.auth import views as auth_views

urlpatterns = [
    # Replace custom login/logout with built-in views
    path('login/', auth_views.LoginView.as_view(template_name='music/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(template_name='music/logout.html'), name='logout'),
    # ... other routes
]

Use this approach only if it aligns with your app's requirements.


内容的提问来源于stack exchange,提问作者Themine Daha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:10:14