博客蓝图

博客蓝图与验证蓝图所使用的技术一样。博客页面应当列出所有的帖子,允许已登录 用户创建帖子,并允许帖子作者修改和删除帖子。

当你完成每个视图时,请保持开发服务器运行。当你保存修改后,请尝试在浏览器中 访问 URL ,并进行测试。

蓝图

定义蓝图并注册到应用工厂。

flaskr/blog.py

1. from flask import (
2. Blueprint, flash, g, redirect, render_template, request, url_for
3. )
4. from werkzeug.exceptions import abort

6. from flaskr.auth import login_required
7. from flaskr.db import get_db

9. bp = Blueprint('blog', __name__)

使用 app.register_blueprint() 在工厂中 导入和注册蓝图。将新代码放在工厂函数的尾部,返回应用之前。

flaskr/__init__.py

1. def create_app():
2. app = ...
3. # existing code omitted

5. from . import blog
6. app.register_blueprint(blog.bp)
7. app.add_url_rule('/', endpoint='index')

9. return app

与验证蓝图不同,博客蓝图没有 url_prefix 。因此 index 视图会用于 / , create 会用于 /create ,以此类推。博客是 Flaskr 的主要 功能,因此把博客索引作为主索引是合理的。

但是,下文的 index 视图的端点会被定义为 blog.index 。一些验证视图 会指定向普通的 index 端点。 我们使用 app.add_url_rule() 关联端点名称 'index' 和 / URL ,这样 url_for('index') 或 url_for('blog.index') 都会有效,会生成同样的 / URL 。

在其他应用中,可能会在工厂中给博客蓝图一个 url_prefix 并定义一个独立的 index 视图,类似前文中的 hello 视图。在这种情况下 index 和 blog.index 的端点和 URL 会有所不同。

索引

索引会显示所有帖子,最新的会排在最前面。为了在结果中包含 user 表中的 作者信息,使用了一个 JOIN 。

flaskr/blog.py

1. @bp.route('/')
2. def index():
3. db = get_db()
4. posts = db.execute(
5. 'SELECT p.id, title, body, created, author_id, username'
6. ' FROM post p JOIN user u ON p.author_id = u.id'
7. ' ORDER BY created DESC'
8. ).fetchall()
9. return render_template('blog/index.html', posts=posts)

flaskr/templates/blog/index.html

1. {% extends 'base.html' %}

3. {% block header %}
4. <h1>{% block title %}Posts{% endblock %}</h1>
5. {% if g.user %}
6. <a class="action" href="{{ url_for('blog.create') }}">New</a>
7. {% endif %}
8. {% endblock %}

10. {% block content %}
11. {% for post in posts %}
12. <article class="post">
13. <header>
14. <div>
15. <h1>{{ post['title'] }}</h1>
16. <div class="about">by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}</div>
17. </div>
18. {% if g.user['id'] == post['author_id'] %}
19. <a class="action" href="{{ url_for('blog.update', id=post['id']) }}">Edit</a>
20. {% endif %}
21. </header>
22. <p class="body">{{ post['body'] }}</p>
23. </article>
24. {% if not loop.last %}
25. <hr>
26. {% endif %}
27. {% endfor %}
28. {% endblock %}

当用户登录后, header 块添加了一个指向 create 视图的连接。当用户是 博客作者时,可以看到一个“ Edit ”连接,指向 update 视图。 loop.last 是一个 Jinja for 循环 内部可用的特殊变量,它用于在每个 博客帖子后面显示一条线来分隔帖子,最后一个帖子除外。

创建

create 视图与 register 视图原理相同。要么显示表单,要么发送内容 已通过验证且内容已加入数据库,或者显示一个出错信息。

先前写的 login_required 装饰器用在了博客视图中,这样用户必须登录以后 才能访问这些视图,否则会被重定向到登录页面。

flaskr/blog.py

1. @bp.route('/create', methods=('GET', 'POST'))
2. @login_required
3. def create():
4. if request.method == 'POST':
5. title = request.form['title']
6. body = request.form['body']
7. error = None

9. if not title:
10. error = 'Title is required.'

12. if error is not None:
13. flash(error)
14. else:
15. db = get_db()
16. db.execute(
17. 'INSERT INTO post (title, body, author_id)'
18. ' VALUES (?, ?, ?)',
19. (title, body, g.user['id'])
20. )
21. db.commit()
22. return redirect(url_for('blog.index'))

24. return render_template('blog/create.html')

flaskr/templates/blog/create.html

1. {% extends 'base.html' %}

3. {% block header %}
4. <h1>{% block title %}New Post{% endblock %}</h1>
5. {% endblock %}

7. {% block content %}
8. <form method="post">
9. <label for="title">Title</label>
10. <input name="title" id="title" value="{{ request.form['title'] }}" required>
11. <label for="body">Body</label>
12. <textarea name="body" id="body">{{ request.form['body'] }}</textarea>
13. <input type="submit" value="Save">
14. </form>
15. {% endblock %}

更新

update 和 delete 视图都需要通过 id 来获取一个 post ,并且 检查作者与登录用户是否一致。为避免重复代码,可以写一个函数来获取 post , 并在每个视图中调用它。

flaskr/blog.py

1. def get_post(id, check_author=True):
2. post = get_db().execute(
3. 'SELECT p.id, title, body, created, author_id, username'
4. ' FROM post p JOIN user u ON p.author_id = u.id'
5. ' WHERE p.id = ?',
6. (id,)
7. ).fetchone()

9. if post is None:
10. abort(404, f"Post id {id} doesn't exist.")

12. if check_author and post['author_id'] != g.user['id']:
13. abort(403)

15. return post

abort() 会引发一个特殊的异常,返回一个 HTTP 状态码。它有一个可选参数, 用于显示出错信息,若不使用该参数则返回缺省出错信息。 404 表示“未找到”, 403 代表“禁止访问”。( 401 表示“未授权”,但是我们重定向到登录 页面来代替返回这个状态码)

check_author 参数的作用是函数可以用于在不检查作者的情况下获取一个 post 。这主要用于显示一个独立的帖子页面的情况,因为这时用户是谁没有关系, 用户不会修改帖子。

flaskr/blog.py

1. @bp.route('/<int:id>/update', methods=('GET', 'POST'))
2. @login_required
3. def update(id):
4. post = get_post(id)

6. if request.method == 'POST':
7. title = request.form['title']
8. body = request.form['body']
9. error = None

11. if not title:
12. error = 'Title is required.'

14. if error is not None:
15. flash(error)
16. else:
17. db = get_db()
18. db.execute(
19. 'UPDATE post SET title = ?, body = ?'
20. ' WHERE id = ?',
21. (title, body, id)
22. )
23. db.commit()
24. return redirect(url_for('blog.index'))

26. return render_template('blog/update.html', post=post)

和所有以前的视图不同, update 函数有一个 id 参数。该参数对应路由中 的 <int:id> 。一个真正的 URL 类似 /1/update 。 Flask 会捕捉到 URL 中的 1 ,确保其为一个 int ,并将其作为 id 参数传递给视图。 如果没有指定 int: 而是仅仅写了 <id> ,那么将会传递一个字符串。 要生成一个指向更新页面的 URL ,需要传递 id 参数给 url_for() : url_for('blog.update', id=post['id']) 。前文的 index.html 文件中 同样如此。

create 和 update 视图看上去是相似的。 主要的不同之处在于 update 视图使用了一个 post 对象和一个 UPDATE 查询代替了一个 INSERT 查询。作为一个明智的重构者,可以使用 一个视图和一个模板来同时完成这两项工作。但是作为一个初学者,把它们分别处理 要清晰一些。

flaskr/templates/blog/update.html

1. {% extends 'base.html' %}

3. {% block header %}
4. <h1>{% block title %}Edit "{{ post['title'] }}"{% endblock %}</h1>
5. {% endblock %}

7. {% block content %}
8. <form method="post">
9. <label for="title">Title</label>
10. <input name="title" id="title"
11. value="{{ request.form['title'] or post['title'] }}" required>
12. <label for="body">Body</label>
13. <textarea name="body" id="body">{{ request.form['body'] or post['body'] }}</textarea>
14. <input type="submit" value="Save">
15. </form>
16. <hr>
17. <form action="{{ url_for('blog.delete', id=post['id']) }}" method="post">
18. <input class="danger" type="submit" value="Delete" onclick="return confirm('Are you sure?');">
19. </form>
20. {% endblock %}

这个模板有两个表单。第一个提交已编辑过的数据给当前页面( /<id>/update )。 另一个表单只包含一个按钮。它指定一个 action 属性,指向删除视图。这个按钮 使用了一些 JavaScript 用以在提交前显示一个确认对话框。

参数 {{ request.form['title'] or post['title'] }} 用于选择在表单显示什么 数据。当表单还未提交时,显示原 post 数据。但是,如果提交了非法数据,然后 需要显示这些非法数据以便于用户修改时,就显示 request.form 中的数据。 request 是又一个自动在模板中可用的变量。

删除

删除视图没有自己的模板。删除按钮已包含于 update.html 之中,该按钮指向 /<id>/delete URL 。既然没有模板,该视图只处理 POST 方法并重定向到 index 视图。

flaskr/blog.py

1. @bp.route('/<int:id>/delete', methods=('POST',))
2. @login_required
3. def delete(id):
4. get_post(id)
5. db = get_db()
6. db.execute('DELETE FROM post WHERE id = ?', (id,))
7. db.commit()
8. return redirect(url_for('blog.index'))

恭喜,应用写完了!花点时间在浏览器中试试这个应用吧。然而,构建一个完整的 应用还有一些工作要做。

下面请阅读 项目可安装化 。