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

如何在Django项目中为单个应用配置多端点URL路由?

解决方案

直接按你现在的写法会导致路由冲突——students/和courses/都会匹配到school.urls里的同一组视图,显然不符合需求。不需要拆分应用,也可以不用新增太多文件,下面提供两种可行方案:

方案一:在同一个school/urls.py中拆分路由集合

这种方式不需要新增文件,只需在原文件中把学生和课程的路由分开定义,然后在根路由中分别引入。

1. 修改config/urls.py

需要用include的元组格式((urlpatterns, app_name))来指定命名空间,避免反向解析时的名称冲突:

from django.urls import path, include
from school.urls import student_urlpatterns, course_urlpatterns

urlpatterns = [
    # 其他已有路由
    path("students/", include((student_urlpatterns, "school"), namespace="students")),
    path("courses/", include((course_urlpatterns, "school"), namespace="courses")),
]

2. 修改school/urls.py

将学生和课程的路由拆分为两个独立的urlpatterns集合:

from django.urls import path
from .views import (
    StudentCreateView, StudentDetailView, StudentListView, StudentUpdateView, StudentDeleteView,
    CourseCreateView, CourseDetailView, CourseListView, CourseUpdateView, CourseDeleteView
)

# 学生专属路由
student_urlpatterns = [
    path("", StudentListView.as_view(), name="list"),
    path("add/", StudentCreateView.as_view(), name="add"),
    path("<int:pk>/", StudentDetailView.as_view(), name="detail"),
    path("<int:pk>/update/", StudentUpdateView.as_view(), name="update"),
    path("<int:pk>/delete/", StudentDeleteView.as_view(), name="delete"),
]

# 课程专属路由
course_urlpatterns = [
    path("", CourseListView.as_view(), name="list"),
    path("add/", CourseCreateView.as_view(), name="add"),
    path("<int:pk>/", CourseDetailView.as_view(), name="detail"),
    path("<int:pk>/update/", CourseUpdateView.as_view(), name="update"),
    path("<int:pk>/delete/", CourseDeleteView.as_view(), name="delete"),
]

后续反向解析时,用students:list或courses:list即可区分不同模块的路由。

方案二:拆分school下的路由文件

如果路由较多,想要更清晰的结构,可以在school应用下创建子路由目录,分别存放学生和课程的路由。

1. 创建路由文件结构

在school目录下新建urls文件夹,里面创建:

  • __init__.py(空文件即可,让Python识别为模块)
  • students.py(学生路由)
  • courses.py(课程路由)

2. 编写school/urls/students.py

from django.urls import path
from ...views import StudentCreateView, StudentDetailView, StudentListView, StudentUpdateView, StudentDeleteView

urlpatterns = [
    path("", StudentListView.as_view(), name="list"),
    path("add/", StudentCreateView.as_view(), name="add"),
    path("<int:pk>/", StudentDetailView.as_view(), name="detail"),
    path("<int:pk>/update/", StudentUpdateView.as_view(), name="update"),
    path("<int:pk>/delete/", StudentDeleteView.as_view(), name="delete"),
]

3. 编写school/urls/courses.py

from django.urls import path
from ...views import CourseCreateView, CourseDetailView, CourseListView, CourseUpdateView, CourseDeleteView

urlpatterns = [
    path("", CourseListView.as_view(), name="list"),
    path("add/", CourseCreateView.as_view(), name="add"),
    path("<int:pk>/", CourseDetailView.as_view(), name="detail"),
    path("<int:pk>/update/", CourseUpdateView.as_view(), name="update"),
    path("<int:pk>/delete/", CourseDeleteView.as_view(), name="delete"),
]

4. 修改config/urls.py

直接引入拆分后的子路由模块,并指定命名空间:

from django.urls import path, include

urlpatterns = [
    # 其他已有路由
    path("students/", include("school.urls.students", namespace="students")),
    path("courses/", include("school.urls.courses", namespace="courses")),
]

注意:要确保school/apps.py中的AppConfig正确设置了name = 'school',否则命名空间会失效。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 13:01:02