如何使用蓝图在烧瓶中做一个url_for?

时间:2016-01-04 21:09:35

标签: python flask

代码:

蓝图:

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')时 它仍然是一个路由错误

1 个答案:

答案 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)