code:
blueprint:
from flask import Blueprint
from flask_restful import Api
################################
### Local Imports ###
################################
profile_api = Blueprint('profile_api', __name__)
api = Api(profile_api)
from .views import *
Run Code Online (Sandbox Code Playgroud)
views:
class ShowProfPic(Resource):
def get(self):
return "hey"
api.add_resource(ShowProfPic, '/get profile picture/',endpoint="showpic")
Run Code Online (Sandbox Code Playgroud)
how do we do a url_for with flask_restful?
because when I do.
url_for('showpic') it's a routing error
and when I do url_for('api.ShowProfPic')
it's also still a routing error
我已经得到答案了。
显然在工作时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")
Run Code Online (Sandbox Code Playgroud)
引用该类ShowProfPic并得到它,endpoint
所以url_for('blueprint_name.endpoint')
就是这样url_for(profile_api.showpic)
| 归档时间: |
|
| 查看次数: |
2228 次 |
| 最近记录: |