【问题标题】:Using Flask Blueprints, how to fix url_for from breaking if a subdomain is specified?使用 Flask 蓝图,如果指定了子域,如何修复 url_for 中断?
【发布时间】:2012-05-19 12:38:42
【问题描述】:

在烧瓶蓝图中,我有:

frontend = Blueprint('frontend', __name__)

到我的索引函数的路径是:

@frontend.route('/')
def index():
  #code

这很好,但是我正在尝试向路由添加一个子域,如下所示:

@frontend.route('/', subdomain='<var>')
def index(var):

但这会破坏应用程序并且浏览器会吐出(除其他外):

werkzeug.routing.BuildError
BuildError: ('frontend.index', {}, None)

frontend.index 在我的代码中的 url_for('frontend.index')

的几个地方被调用

当我包含子域时,如何让 url_for 工作?在文件中我能找到并且我认为可能相关的唯一内容是http://flask.pocoo.org/docs/api/

为了集成应用程序,Flask 有一个钩子来拦截 URL 构建 通过 Flask.build_error_handler 出错。 url_for 函数结果 在当前应用程序没有给定 URL 时的 BuildError 中 端点和值。当它这样做时, current_app 调用它的 build_error_handler 如果不是None,可以返回一个字符串到 使用 url_for 的结果(而不是 url_for 的默认值 raise BuildError 异常)或重新引发异常。一个例子:

def external_url_handler(error, endpoint, **values):
    "Looks up an external URL when `url_for` cannot build a URL."
    # This is an example of hooking the build_error_handler.
    # Here, lookup_url is some utility function you've built
    # which looks up the endpoint in some external URL registry.
    url = lookup_url(endpoint, **values)
    if url is None:
        # External lookup did not have a URL.
        # Re-raise the BuildError, in context of original traceback.
        exc_type, exc_value, tb = sys.exc_info()
        if exc_value is error:
            raise exc_type, exc_value, tb
        else:
            raise error
    # url_for will use this result, instead of raising BuildError.
    return url

app.build_error_handler = external_url_handler

但是,我是 python(和编程)的新手,无法理解我将把这段代码放在哪里,或者在发生构建错误时如何调用该函数。

任何见解将不胜感激:)

【问题讨论】:

  • 您是否尝试将_external=True 添加到您的 url_for() 调用中?
  • @chrickso:看我的回答。看起来,您只需要提供不同的方法名称。

标签: python routes flask


【解决方案1】:

url_for 中添加蓝图名称。示例:

url_for('pay_sermepa.sermepa_cancel', _external=True)
  • pay_sermepa: 蓝图名称
  • sermepa_cancel:路线

【讨论】:

    【解决方案2】:

    首先,要使用子域,您需要为 SERVER_NAME configuration 设置一个值:

    app.config['SERVER_NAME'] = 'example.net'
    

    你有这样的看法:

    frontend = Blueprint('frontend', __name__)
    @frontend.route('/', subdomain='<var>')
    def index(var):
        return ...
    

    为了重构这个视图的 URL,Flask 需要一个 var 的值。 url_for('frontend.index') 将失败,因为它没有足够的值。使用上面的 SERVER_NAME,url_for('frontend.index', var='foo') 将返回 http://foo.example.net/

    【讨论】:

      【解决方案3】:

      我认为这不是 Flask 的问题。

      您提供了两个具有相同方法名称的函数:

      @frontend.route('/')
      def index():
        #code
      

      @frontend.route('/', subdomain='<var>')
      def index(var):
      

      它们的包装方式不同,但是当调用 flask.build_url 时,由于函数名重载而抛出异常。 乍一看,这似乎是不正确的。

      尝试为第二个提供不同的函数名称,例如

      @frontend.route('/', subdomain='<var>')
      def index_var(var):
      

      这可能会解决您的问题。不过我还没有测试过。

      【讨论】:

        猜你喜欢
        • 2015-03-10
        • 2019-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多