Python 链接到具有url_的Flask静态文件
如何使用Flask中的Python 链接到具有url_的Flask静态文件,python,flask,jinja2,Python,Flask,Jinja2,如何使用Flask中的url\u引用文件夹中的文件?例如,我在static文件夹中有一些静态文件,其中一些可能位于子文件夹中,例如static/bootstrap 当我试图从static/bootstrap提供文件时,我得到了一个错误 <link rel=stylesheet type=text/css href="{{ url_for('static/bootstrap', filename='bootstrap.min.css') }}"> 我可以引用不在子文件夹中的文件
url\u引用文件夹中的文件?例如,我在static
文件夹中有一些静态文件,其中一些可能位于子文件夹中,例如static/bootstrap
当我试图从static/bootstrap
提供文件时,我得到了一个错误
<link rel=stylesheet type=text/css href="{{ url_for('static/bootstrap', filename='bootstrap.min.css') }}">
我可以引用不在子文件夹中的文件
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='bootstrap.min.css') }}">
对于
,使用url\u引用静态文件的正确方法是什么?如何使用url\u for
生成指向任何级别静态文件的url?默认情况下,您拥有用于静态文件的url。应用程序还具有以下参数:
static\u url\u path
:可用于为web上的静态文件指定不同的路径。默认为静态文件夹
文件夹的名称
static\u文件夹
:包含静态文件的文件夹,应在static\u url\u路径
处提供。默认为应用程序根路径中的“静态”文件夹
这意味着filename
参数将采用指向static\u文件夹中文件的相对路径
,并将其转换为与static\u url\u default
组合的相对路径:
url_for('static', filename='path/to/file')
将文件路径从static\u folder/path/to/file
转换为url路径static\u url\u default/path/to/file
因此,如果您想从static/bootstrap
文件夹中获取文件,请使用以下代码:
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='bootstrap/bootstrap.min.css') }}">
将转换为(使用默认设置):
另外,请查看。默认情况下,您拥有用于静态文件的。应用程序还具有以下参数:
static\u url\u path
:可用于为web上的静态文件指定不同的路径。默认为静态文件夹
文件夹的名称
static\u文件夹
:包含静态文件的文件夹,应在static\u url\u路径
处提供。默认为应用程序根路径中的“静态”文件夹
这意味着filename
参数将采用指向static\u文件夹中文件的相对路径
,并将其转换为与static\u url\u default
组合的相对路径:
url_for('static', filename='path/to/file')
将文件路径从static\u folder/path/to/file
转换为url路径static\u url\u default/path/to/file
因此,如果您想从static/bootstrap
文件夹中获取文件,请使用以下代码:
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='bootstrap/bootstrap.min.css') }}">
将转换为(使用默认设置):
另请看。在我的例子中,我对nginx配置文件有特殊说明:
location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
try_files $uri =404;
}
所有客户端都收到了“404”,因为nginx对Flask一无所知
Linux上的主要配置文件是/etc/nginx/nginx.conf
。在Windows上可能类似。在我的情况下,我对nginx配置文件有特殊说明:
location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
try_files $uri =404;
}
所有客户端都收到了“404”,因为nginx对Flask一无所知
Linux上的主要配置文件是/etc/nginx/nginx.conf
。在Windows上可能类似