【问题标题】:Is there a way to get the full url in flask_restful from a resource [duplicate]有没有办法从资源中获取 flask_restful 中的完整 url [重复]
【发布时间】:2019-04-22 16:49:26
【问题描述】:

假设我有一个资源,它什么都不做,只是将 url 返回到控制台

from app import api

class StaticFiles(Resource):
    def get(self):
        return api.url_for(self) # this only provides the resource url 

如果请求是 http://localhost:5000/static 上面的代码返回 /static 我在找http://localhost:5000/static

通常我使用请求,但资源中没有请求。 我正在寻找几乎相当于request.base_url

【问题讨论】:

    标签: python rest url flask flask-restful


    【解决方案1】:

    request 是一个全局对象,您可以通过 importing request from flask 访问它:

    from flask import request
    from app import api
    
    class StaticFiles(Resource):
    def get(self):
        return(request.base_url)
    

    这里有很多不错的 url 替代格式,请参阅this answer 了解更多详情。

    【讨论】:

    • 哦,什么,谢谢,尽管请求只能与路由装饰器一起使用。我真是个白痴,我猜它是一个线程本地对象。你不会相信我尝试过的事情。
    • @EvrenBingøl 没问题,我不认为这很明显,我也不认为这是一个重复的问题,因为其他答案都没有真正告诉你如何掌握@首先是987654328@!
    猜你喜欢
    • 2015-10-18
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    相关资源
    最近更新 更多