首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用蓝图在flask restful中做一个url_for?

如何使用蓝图在flask restful中做一个url_for?
EN

Stack Overflow用户
提问于 2016-01-05 05:09:35
回答 1查看 1.3K关注 0票数 4

代码:

蓝图:

代码语言:javascript
复制
from flask import Blueprint
from flask_restful import Api
################################

### Local Imports ###

################################


profile_api = Blueprint('profile_api', __name__)
api = Api(profile_api)
from .views import *

视图:

代码语言:javascript
复制
class ShowProfPic(Resource):
    def get(self):
        return "hey"

api.add_resource(ShowProfPic, '/get profile picture/',endpoint="showpic")  

我们怎么用flask_restful?url_for,因为当我这样做的时候。

这是一个路由错误,当我使用url_for('api.ShowProfPic')时,它仍然是一个路由错误

EN

回答 1

Stack Overflow用户

发布于 2016-01-05 05:53:06

我已经知道答案了。

显然,在使用blueprints

访问flask_restful's url_for的方法是

url_for('blueprint_name.endpoint)

意味着必须在资源上指定终结点

所以使用上面的例子:

代码语言:javascript
复制
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)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34599881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档