Flask API 实战:留言板功能
今天我们来实现一个留言板功能。
接口设计
我们需要两个接口:
GET /api/comments- 获取留言列表POST /api/comments- 发布新留言
代码实现
@app.route('/api/comments', methods=['GET'])
def get_comments():
page = request.args.get('page', '/')
# 从数据库获取留言...
return jsonify({'comments': comments})
小结
通过这个练习,我们学会了:
- RESTful API 设计原则
- Flask 路由和请求处理
- JSON 数据格式