代码:
蓝图:
from flask import Blueprint
from flask_restful import Api
################################
### Local Imports ###
################################
profile_api = Blueprint('profile_api', __name__)
api = Api(profile_api)
from .views import *
的观点:
class ShowProfPic(Resource):
def get(self):
return "hey"
api.add_resource(ShowProfPic, '/get profile picture/',endpoint="showpic")
我们如何使用url_for
进行flask_restful?
因为当我这样做的时候。
url_for('showpic')
这是一个路由错误
当我url_for('api.ShowProfPic')
时
它仍然是一个路由错误
答案 0 :(得分:1)
我得到了答案。
显然在使用blueprints
访问flask_restful's
url_for
的方法是
url_for('blueprint_name.endpoint)
表示必须在资源上指定端点
所以使用上面的例子:
profile_api = Blueprint('profile_api', __name__)
api = Api(profile_api)
from .views import *
class ShowProfPic(Resource):
def get(self):
return "hey"
api.add_resource(ShowProfPic, '/get profile picture/',endpoint="showpic")
引用ShowProfPic
类并获取endpoint
这是url_for('blueprint_name.endpoint')
这是url_for(profile_api.showpic)