显示页面讨论过去修订反向链接回到顶部 本页面只读。您可以查看源文件,但不能更改它。如果您觉得这是系统错误,请联系管理员。 ====== 第三十五章:Flask Web开发 ====== ===== 本章目标 ===== 完成本章学习后,你将能够: * 创建Flask应用 * 定义路由和视图 * 处理请求和响应 * 使用模板 ===== 快速开始 ===== <code python> from flask import Flask, jsonify, request, render_template app = Flask(__name__) @app.route('/') def hello(): return 'Hello, World!' @app.route('/user/<name>') def user(name): return f'Hello, {name}!' @app.route('/api/data', methods=['GET', 'POST']) def data(): if request.method == 'POST': data = request.json return jsonify({'received': data}), 201 return jsonify({'message': 'GET request'}) if __name__ == '__main__': app.run(debug=True) </code> ===== 模板渲染 ===== <code python> from flask import render_template @app.route('/hello/<name>') def hello_template(name): return render_template('hello.html', name=name) </code> ```html <!-- templates/hello.html --> <!DOCTYPE html> <html> <head><title>Hello</title></head> <body> <h1>Hello, {{ name }}!</h1> {% if name == 'admin' %} <p>Welcome, administrator!</p> {% endif %} </body> </html> ``` ===== 本章练习 ===== 1. 创建RESTful API 2. 实现用户登录系统 3. 连接数据库 下一章:[[python_course:chapter36|第三十六章:Django Web开发]] 登录 Detach Close 该主题尚不存在 您访问的页面并不存在。如果允许,您可以使用创建该页面按钮来创建它。 python/chapter35.txt 最后更改: 2026/04/09 14:41由 张叶安 登录