win主机wordpress伪静态,南宁网站seo顾问,产品展示网站模板php,企业网站规划方案第一步#xff1a;新建flask项目
参考使用pycharm新建一个项目 打开pycharm#xff0c;根据下面图中箭头顺序#xff0c;新建一个flask的项目#xff1b;
第二步#xff1a;运行项目#xff0c;
安装成功以后#xff0c;会有个app.py文件#xff0c;打开以后#…第一步新建flask项目
参考使用pycharm新建一个项目 打开pycharm根据下面图中箭头顺序新建一个flask的项目
第二步运行项目
安装成功以后会有个app.py文件打开以后运行它 可以使用右上角的运行按钮运行也可以在文件内右击运行如下图
运行以后会出现访问地址这时候浏览器就能打开访问了
第三步编写自己的第一个接口
from flask import Flask, request, jsonifyapp Flask(__name__)# 假设博客文章数据存储在一个列表中
posts [{id: 1, title: Hello World, content: This is the first blog post.},{id: 2, title: Introduction to Flask, content: A tutorial on using Flask to build web applications.}
]# 获取所有博客文章列表
app.route(/api/posts, methods[GET])
def get_posts():return jsonify(posts)# 获取单篇博客文章
app.route(/api/posts/int:post_id, methods[GET])
def get_post(post_id):post next((p for p in posts if p[id] post_id), None)if post:return jsonify(post)else:return jsonify({message: Post not found}), 404# 创建新的博客文章
app.route(/api/posts, methods[POST])
def create_post():data request.get_json()if title in data and content in data:new_post {id: len(posts) 1,title: data[title],content: data[content]}posts.append(new_post)return jsonify(new_post), 201else:return jsonify({message: Invalid data}), 400# 更新博客文章
app.route(/api/posts/int:post_id, methods[PUT])
def update_post(post_id):post next((p for p in posts if p[id] post_id), None)if post:data request.get_json()post[title] data.get(title, post[title])post[content] data.get(content, post[content])return jsonify(post)else:return jsonify({message: Post not found}), 404# 删除博客文章
app.route(/api/posts/int:post_id, methods[DELETE])
def delete_post(post_id):global postsposts [p for p in posts if p[id] ! post_id]return , 204if __name__ __main__:app.run()这里假设数据存储在一个列表里并不是从数据库中取出的先跑起来哈下一篇我再分享怎么连接数据库
这时候访问http://127.0.0.1:5000/api/posts,就能获取到数据了