> 本章配套：理论课 `theory/ch01.html`　·　PPT `slides/ch01.pptx`　·　练习 `exercises/ch01.html`

# lab01_flask_routing

Flask 第一课：路由四件套。端口 **5110**　地址 http://127.0.0.1:5110/

> 一步一步带截图的操作手册见 **walkthrough.html**（12 步，全部本机实拍）。

## 1. Run it

| 方式 | 操作 |
|---|---|
| PyCharm | 右上角运行配置选 `lab01_flask_routing（5110）` → 点绿三角 |
| 双击 | 本目录 `run.bat`（自动用项目根的 `.venv`） |
| 命令行 | `..\.venv\Scripts\python.exe app.py` |

成功标志：终端出现 `Running on http://127.0.0.1:5110`。

## 2. Routes

| 地址 | 学到什么 |
|---|---|
| `/` | 固定路由：一个地址对一个函数 |
| `/user/<name>` | 动态路由：URL 里的值当参数 |
| `/post/<int:post_id>` | int 转换器：`/post/abc` 进不来（404） |
| `/redirect` | `redirect(url_for(...))` 跳转 |
| `/nothing` | `abort(404)` 主动抛 404 |

## 3. Check

- [ ] 右下角显示 `Python 3.13 (03_practicum_labs)`
- [ ] 终端出现 `Running on http://127.0.0.1:5110`
- [ ] `/`、`/user/xiefucai`、`/post/1` 都能打开
- [ ] `/post/abc`、`/nothing` 返回 404

## 4. Troubleshooting

| 现象 | 解决 |
|---|---|
| 黄条「未为项目配置 Python 解释器」 | 见 walkthrough 第 2–6 步，路径指到 `.venv\Scripts\python.exe` |
| `ModuleNotFoundError: flask` | 解释器选错，重配到项目 `.venv` |
| `Address already in use` | 运行面板点 Stop（红方块） |
| 中文乱码 | 运行配置加 `PYTHONIOENCODING=utf-8` |

## 5. Files

`app.py` 代码 ｜ `notes.md` 知识点 ｜ `run.bat` 启动 ｜ `screenshots/` 12 张截图 ｜ `walkthrough.html` 图文手册