python config_error ai_generated true

werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'index' with values ['_external']. Did you forget to specify values ['host']?

ID: python/flask-url-for-external-required

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-08-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.9 active
3.10 active

Root Cause

Flask 的 url_for 函数在生成外部 URL 时缺少 host 参数

generic

中文

Flask 的 url_for 函数在生成外部 URL 时缺少 host 参数

Workarounds

  1. 95% success 在应用配置中设置 SERVER_NAME
    app.config['SERVER_NAME'] = 'example.com'
  2. 90% success 显式传递 host 参数
    url_for('index', _external=True, host='example.com')

Dead Ends

Common approaches that don't work:

  1. 移除 _external 参数 60% fail

    生成的 URL 可能不完整

  2. 硬编码主机名 70% fail

    不灵活,不同环境需要修改