flask-restful 使用 flask-restful第三方庫 from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class HelloWorldResource(Resource):
? ? def get(self):
? ? ? ? ? ?return {'hello':'world'}
? ? def post(self):
? ? ? ? ? ?return {'msg':‘post hello wolrd’}
api.add_resource(HelloworldResource,'/') 方式完成restful請求,flask_restful可以直接返回字典
flask-rest? 采用
from flask import Buleprint
api=Blueprint('api',__name__)?
@api.route('/posts/',methods=['get','post']) 方式注冊路由@permission_required(Permission.WRITE)
def new_post():
? ? post = Post.from_json(request.json)
? ? post.author = g.current_user
? ? db.session.add(post)
? ? db.session.commit()
? ? return jsonify(post.to_json()), 201, \ {'Location': url_for('api.get_post', id=post.id)}
原生flask 使用 jsonify返回json格式數(shù)據(jù)
藍圖注冊到app實例上
from .api import api as api_blueprint文章來源:http://www.zghlxwxcb.cn/news/detail-422384.html
app.register_blueprint(api_blueprint, url_prefix='/api/v1')文章來源地址http://www.zghlxwxcb.cn/news/detail-422384.html
到了這里,關(guān)于flask-restful 和 blueprint的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!