代码之家  ›  专栏  ›  技术社区  ›  Evren Bingøl

有没有办法从资源[duplicate]获取flask\u restful中的完整url?

  •  1
  • Evren Bingøl  · 技术社区  · 6 年前

    假设我有一个资源,它不做任何事情,但将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 上面的代码返回/静态 我在找 http://localhost:5000/static

    通常我使用请求,但资源中没有请求。 我要找相当于 request.base_url

    1 回复  |  直到 6 年前
        1
  •  1
  •   Rob Bricheno    6 年前

    request 是一个全局对象,您可以通过 import 惯性导航与制导 请求 flask :

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

    这里有很多不错的url替代格式,请参见 this answer 更多细节。