Skip to content

Commit 183e1f4

Browse files
committed
Fix outdated docs
1 parent 4aa1654 commit 183e1f4

4 files changed

Lines changed: 42 additions & 25 deletions

File tree

chapters/1-preparation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ $ source .venv/bin/activate # Windows 使用 .venv\Scripts\activate 命令
279279
然后使用 uv 安装依赖:
280280

281281
```shell
282-
$ uv pip install flask
282+
(.venv) $ uv pip install flask
283283
```
284284

285285
除了像这样使用 uv 来代理 pip 操作,你也可以完成使用 uv 自带的依赖管理系统,详情可以访问文档 <https://docs.astral.sh/uv/guides/projects/> 了解。

chapters/5-database.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ SQLITE_PREFIX = 'sqlite:///' if sys.platform.startswith('win') else 'sqlite:////
7171

7272
app = Flask(__name__)
7373
app.config['SQLALCHEMY_DATABASE_URI'] = SQLITE_PREFIX + str(Path(app.root_path) / 'data.db')
74-
db = SQLAlchemy(app)
74+
75+
db = SQLAlchemy(app, model_class=Base)
7576
```
7677

7778
如果你固定在某一个操作系统上进行开发,部署时也使用相同的操作系统,那么可以不用这么做,直接根据你的需要写出前缀即可。

chapters/8-auth.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ False
2525
我们在存储用户信息的 `User` 模型类添加 `username` 字段和 `password_hash` 字段,分别用来存储登录所需的用户名和密码散列值,同时添加两个方法来实现设置密码和验证密码的功能:
2626

2727
```python
28+
from sqlalchemy import String
29+
from sqlalchemy.orm import Mapped, mapped_column
2830
from werkzeug.security import generate_password_hash, check_password_hash
2931

3032

3133
class User(db.Model):
32-
id = db.Column(db.Integer, primary_key=True)
33-
name = db.Column(db.String(20))
34-
username = db.Column(db.String(20)) # 用户名
35-
password_hash = db.Column(db.String(128)) # 密码散列值
34+
__tablename__ = 'user' # 定义表名称
35+
id: Mapped[int] = mapped_column(primary_key=True) # 主键
36+
name: Mapped[str] = mapped_column(String(20)) # 名字
37+
username: Mapped[str] = mapped_column(String(20)) # 用户名
38+
password_hash: Mapped[str] = mapped_column(String(128)) # 密码散列值
3639

3740
def set_password(self, password): # 用来设置密码的方法,接受密码作为参数
3841
self.password_hash = generate_password_hash(password) # 将生成的密码保持到对应字段
@@ -149,9 +152,9 @@ def login():
149152
flash('Invalid input.')
150153
return redirect(url_for('login'))
151154

152-
user = db.session.execute(select(User)).scalar()
153-
# 验证用户名和密码是否一致
154-
if username == user.username and user.validate_password(password):
155+
user = db.session.get(select(User).filter_by(username=username)).scalar()
156+
# 验证密码是否一致
157+
if user is not None and user.validate_password(password):
155158
login_user(user) # 登入用户
156159
flash('Login success.')
157160
return redirect(url_for('index')) # 重定向到主页
@@ -285,7 +288,7 @@ def settings():
285288
current_user.name = name # 更新当前用户的名字
286289
# current_user 会返回当前登录用户的数据库记录对象
287290
# 等同于下面的用法
288-
# user = db.session.execute(select(User)).scalar()
291+
# user = db.session.get(User, current_user.id)
289292
# user.name = name
290293
db.session.commit()
291294
flash('Settings updated.')

chapters/9-organize.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ app.register_blueprint(auth_bp)
102102
from flask import Flask
103103

104104
def create_app():
105-
app = Flask(__name__)
106-
return app
105+
app = Flask(__name__) # 创建程序实例
106+
return app # 返回程序实例
107107
```
108108

109109
> **提示** 按照惯例,工厂函数一般会被命名为 create_app 或 make_app。
@@ -259,16 +259,17 @@ $ touch blueprints/auth.py blueprints/main.py # 创建蓝本模块
259259

260260
我们把这个包称为程序包,包里目前包含的模块和作用如下表所示:
261261

262-
| 模块 | 作用 |
263-
| ------------------ | -------------- |
264-
| \_\_init\_\_.py | 包构造文件,包含工厂函数定义 |
265-
| settings.py | 程序配置 |
266-
| errors.py | 错误处理函数 |
267-
| models.py | 模型类 |
268-
| commands.py | 命令函数 |
269-
| extensions.py | 扩展相关代码 |
270-
| blueprints/main.py | main 蓝本和相关视图函数 |
271-
| blueprints/auth.py | auth 蓝本和相关视图函数 |
262+
| 模块 | 作用 |
263+
| -------------------------- | -------------- |
264+
| \_\_init\_\_.py | 包构造文件,包含工厂函数定义 |
265+
| settings.py | 程序配置 |
266+
| errors.py | 错误处理函数 |
267+
| models.py | 模型类 |
268+
| commands.py | 命令函数 |
269+
| extensions.py | 扩展相关代码 |
270+
| blueprints/\_\_init\_\_.py | 蓝本子包的构造文件,内容为空 |
271+
| blueprints/main.py | main 蓝本和相关视图函数 |
272+
| blueprints/auth.py | auth 蓝本和相关视图函数 |
272273

273274
> **提示** 除了包构造文件外,其他的模块文件名你可以自由修改,比如 settings.py 也可以叫 config.py。
274275
@@ -334,6 +335,8 @@ def load_user(user_id):
334335
from watchlist.models import User
335336
user = db.session.get(User, int(user_id))
336337
return user
338+
339+
login_manager.login_view = 'login'
337340
```
338341

339342
因为 models.py 模块需要导入存放在 extensions.py 中的 db 对象,为了避免循环依赖,`load_user()` 函数中使用的 User 模型类在函数内进行导入。
@@ -377,7 +380,7 @@ def create_app(config_name='development'):
377380
return app
378381
```
379382

380-
我们为两个蓝本在 blueprints 子目录下分别创建了对应的模块。以认证蓝本为例,我们把 auth 蓝本的定义和相关视图函数放到了 auth.py 模块下。
383+
我们为两个蓝本在 blueprints 子包下分别创建了对应的模块(记得为 blueprints 子包创建一个构造文件 `__init__.py`。以认证蓝本为例,我们把 auth 蓝本的定义和相关视图函数放到了 auth.py 模块下。
381384

382385
*watchlist/blueprints/auth.py:认证蓝本*
383386

@@ -397,10 +400,10 @@ def logout():
397400
...
398401
```
399402

400-
其他代码则按照分类分别放到各自的模块中,这里不再给出代码,具体可参考[源码仓库](https://github.com/helloflask/watchlist)。在移动代码之后,注意添加并更新导入语句,比如使用下面的导入语句来导入程序实例
403+
其他代码则按照分类分别放到各自的模块中,这里不再给出代码,具体可参考[源码仓库](https://github.com/helloflask/watchlist)。在移动代码之后,注意添加并更新导入语句,比如使用下面的导入语句来导入工厂函数
401404

402405
```python
403-
from watchlist import app
406+
from watchlist import create_app
404407
```
405408

406409
使用下面的导入语句来导入扩展对象模型类和扩展对象:
@@ -452,11 +455,20 @@ def bad_request(e):
452455
FLASK_APP=watchlist
453456
```
454457

458+
为了更直观,我们也可以在项目根目录创建一个程序入口脚本,将其命名为 app.py:
459+
460+
```python
461+
from watchlist import create_app
462+
463+
app = create_app(config_name='development')
464+
```
465+
455466
最终的项目文件结构如下所示:
456467

457468
```
458469
watchlist
459470
├── .flaskenv
471+
├── app.py # 可选的入口脚本
460472
└── watchlist # 程序包
461473
├── __init__.py
462474
├── commands.py
@@ -465,6 +477,7 @@ watchlist
465477
├── settings.py
466478
├── extensions.py
467479
├── blueprints
480+
│ ├── __main__.py
468481
│ ├── main.py
469482
│ └── auth.py
470483
├── static

0 commit comments

Comments
 (0)