Files
djangoadmin-wiki/DjangoAdmin_Django_EleVue/zh/volume2/url.md
T
2026-09-12 23:12:15 +08:00

161 lines
4.9 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
urlpatterns = [
# 查询案例分页列表
path('page', views.ExamplePageView.as_view()),
# 查询案例数据列表(支持筛选)
path('list', views.ExampleListView.as_view()),
# 查询案例详情
path('detail/<int:id>', views.ExampleDetailView.as_view()),
# 添加案例
path('add', views.ExampleAddView.as_view()),
# 更新案例
path('update', views.ExampleUpdateView.as_view()),
# 删除案例
path('delete/<str:id>', views.ExampleDeleteView.as_view()),
# 设置案例状态
path('status', views.ExampleStatusView.as_view()),
# 查询案例数据列表
path('list', 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 参数使用尖括号语法定义。