擅长:python、mysql、java
<p>我猜你用的是烧瓶的WSGI。在这种情况下,Flask不知道<code>SCRIPT_NAME</code>env变量,因为通常HTTP服务器的工作是设置<code>SCRIPT_NAME</code>(以及<code>PATH_INFO</code>)。在</p>
<p>Flask使用此env var设置<code>script_root</code>属性(来自<a href="https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/wrappers.py#L552" rel="nofollow">^{<cd5>}</a>类的相关函数):</p>
<pre><code>@cached_property
def script_root(self):
"""The root path of the script without the trailing slash."""
raw_path = wsgi_decoding_dance(self.environ.get('SCRIPT_NAME') or '',
self.charset, self.encoding_errors)
return raw_path.rstrip('/')
</code></pre>
<p>如果要手动设置,可以使用中间件(从<a href="http://flask.pocoo.org/snippets/35/" rel="nofollow">this snippet</a>)来实现:</p>
^{pr2}$
<p>在此之后,<code>{{ request.script_root }}</code>将在模板中<code>/myapp</code>。在</p>