Files
djangoadmin-wiki/DjangoAdmin_Django_NaiveVue/zh/volume2/url.md
T
2026-09-13 18:12:07 +08:00

221 lines
7.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# URL 路由配置
URL 路由是模块开发的最后一步,负责将 URL 路径映射到视图类。项目使用 Django 的 `path()` 函数定义路由,通过 `include()` 将模块路由注册到主路由。
## 文件位置
```
application/example/urls.py # 模块路由
application/urls.py # 主路由
```
## 模块路由配置
### 完整代码
```python
from django.urls import path
from application.example import views
"""
案例模块路由配置
=============================================================
URL端点概览:
------------------------------------------------------------------------
| 功能说明 | 请求方法 | URL路径 | 视图类 |
|-----------------|----------|-----------------------------|-------------------------|
| 分页查询 | GET | /example/page | ExamplePageView |
| 列表查询 | GET | /example/list | ExampleListView |
| 详情查询 | GET | /example/detail/<id> | ExampleDetailView |
| 添加案例 | POST | /example/add | ExampleAddView |
| 更新案例 | PUT | /example/update | ExampleUpdateView |
| 删除案例 | DELETE | /example/delete/<ids>| ExampleDeleteView |
| 设置状态 | PUT | /example/status | ExampleStatusView |
| 获取列表 | GET | /example/data | ExampleDataView |
------------------------------------------------------------------------
URL参数说明:
- id: int类型,案例ID(用于查询详情)
- id: str类型,逗号分隔的案例ID字符串(用于批量删除)
使用示例:
1. 分页查询:GET /api/example/page?pageNo=1&pageSize=10
2. 列表查询:GET /api/example/list
3. 详情查询:GET /api/example/detail/5
4. 添加案例:POST /api/example/add
5. 更新案例:PUT /api/example/update
6. 删除案例:DELETE /api/example/delete/1,2,3
7. 设置状态:PUT /api/example/status
8. 获取列表:GET /api/example/data
"""
# 案例模块路由配置
urlpatterns = [
# 查询案例分页列表
# URL: /example/page
# 请求方法: GET
# 功能: 获取案例分页数据,支持按案例名称、案例类型、案例状态等条件筛选
path('page', views.ExamplePageView.as_view()),
# 查询案例数据列表(支持筛选)
# URL: /example/list
# 请求方法: GET
# 功能: 获取所有案例的列表数据,支持按条件筛选,用于下拉选择等前端组件
path('list', views.ExampleListView.as_view()),
# 查询案例详情
# URL: /example/detail/<int:id>
# 请求方法: GET
# URL参数: id - 案例ID(整数类型)
# 功能: 根据ID获取指定案例的详细信息
path('detail/<int:id>', views.ExampleDetailView.as_view()),
# 添加案例
# URL: /example/add
# 请求方法: POST
# 功能: 创建新的案例记录,请求体需包含案例信息(name、avatar、type、status、sort等)
path('add', views.ExampleAddView.as_view()),
# 更新案例
# URL: /example/update
# 请求方法: PUT
# 功能: 更新现有案例信息,请求体需包含案例ID和更新内容
path('update', views.ExampleUpdateView.as_view()),
# 删除案例
# URL: /example/delete/<str:id>
# 请求方法: DELETE
# URL参数: id - 案例ID字符串,支持单个ID或逗号分隔的多个ID(如 "1,2,3")
# 功能: 删除指定案例(逻辑删除,设置is_delete=True)
path('delete/<str:id>', views.ExampleDeleteView.as_view()),
# 设置案例状态
# URL: /example/status
# 请求方法: PUT
# 功能: 更新案例的启用/禁用状态
path('status', views.ExampleStatusView.as_view()),
# 查询案例数据列表
# URL: /example/data
# 请求方法: GET
# 功能: 获取所有启用案例的列表数据,用于下拉选择等前端组件
path('data', views.ExampleDataView.as_view()),
]
```
### 路由说明
| URL 路径 | HTTP 方法 | 视图类 | 说明 |
|:----|:----|:----|:----|
| `page` | GET | `ExamplePageView` | 分页查询 |
| `list` | GET | `ExampleListView` | 列表查询(不分页) |
| `detail/<int:id>` | GET | `ExampleDetailView` | 详情查询 |
| `add` | POST | `ExampleAddView` | 添加记录 |
| `update` | PUT | `ExampleUpdateView` | 更新记录 |
| `delete/<str:id>` | DELETE | `ExampleDeleteView` | 删除记录 |
| `status` | PUT | `ExampleStatusView` | 状态更新 |
| `list` | GET | `ExampleDataView` | 数据列表(下拉选择) |
### URL 参数类型
```python
# 整数参数
path('detail/<int:id>', views.ExampleDetailView.as_view())
# 字符串参数(支持逗号分隔的多个ID)
path('delete/<str:id>', views.ExampleDeleteView.as_view())
```
| 参数类型 | 说明 | 示例 |
|:----|:----|:----|
| `<int:id>` | 整数参数 | `/example/detail/5` |
| `<str:id>` | 字符串参数 | `/example/delete/1,2,3` |
### 类视图转换
```python
path('page', views.ExamplePageView.as_view())
```
Django 的类视图需要调用 `.as_view()` 方法将其转换为视图函数,这样才能被 `path()` 识别。
## 主路由注册
### 完整代码
```python
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
# Django 自带的后台管理系统路由
path('admin/', admin.site.urls),
# 登录模块路由
path('', include('application.login.urls')),
# 系统主页模块路由
path('index/', include('application.index.urls')),
# 文件上传模块路由
path('upload/', include('application.upload.urls')),
# 职级管理模块路由
path('level/', include('application.level.urls')),
# 岗位管理模块路由
path('position/', include('application.position.urls')),
# 案例模块路由
path('example/', include('application.example.urls')),
]
```
### include() 函数
```python
path('example/', include('application.example.urls'))
```
| 参数 | 说明 |
|:----|:----|
| `'example/'` | URL 前缀,所有以 `/example/` 开头的请求都会被转发 |
| `include('application.example.urls')` | 包含模块的路由配置 |
`include()` 函数会自动去掉前缀,将剩余部分传递给模块路由。例如:
- 请求 URL:`/example/page`
- 去掉前缀 `/example/` 后:`/page`
- 匹配模块路由:`path('page', views.ExamplePageView.as_view())`
## 完整 URL 路径
注册到主路由后,完整的 API 路径如下:
| 功能 | HTTP 方法 | 完整 URL |
|:----|:----|:----|
| 分页查询 | GET | `/api/example/page?pageNo=1&pageSize=10` |
| 列表查询 | GET | `/api/example/list` |
| 详情查询 | GET | `/api/example/detail/5` |
| 添加记录 | POST | `/api/example/add` |
| 更新记录 | PUT | `/api/example/update` |
| 删除记录 | DELETE | `/api/example/delete/1,2,3` |
| 状态更新 | PUT | `/api/example/status` |
| 数据列表 | GET | `/api/example/list` |
注意:前端请求的 URL 以 `/api` 开头,这是因为 Vite 开发服务器配置了代理,将 `/api` 转发到 Django 后端。
## 开发要点
1. **模块路由文件只定义相对路径**:不包含模块前缀
2. **主路由使用 `include()` 注册模块路由**:实现路由的模块化管理
3. **类视图必须调用 `.as_view()`**:转换为视图函数
4. **URL 参数使用尖括号语法**:`<int:id>` 或 `<str:id>`
5. **删除接口使用字符串参数**:支持逗号分隔的多个 ID
6. **路由注册后需要在 `application/urls.py` 中添加 include**:否则无法访问
## 总结
URL 路由使用 Django 的 `path()` 定义,通过 `include()` 将模块路由注册到主路由。模块路由文件只定义相对路径,主路由负责添加模块前缀。类视图需要调用 `.as_view()` 转换为视图函数,URL 参数使用尖括号语法定义。