Python 使用 url_for 链接到 Flask 静态文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16351826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:22:29  来源:igfitidea点击:

Link to Flask static files with url_for

pythonflaskjinja2

提问by user1431282

How do you use url_forin Flask to reference a file in a folder? For example, I have some static files in the staticfolder, some of which may be in subfolders such as static/bootstrap.

你如何url_for在 Flask 中使用来引用文件夹中的文件?例如,我在static文件夹中有一些静态文件,其中一些可能在子文件夹中,例如static/bootstrap.

When I try to serve a file from static/bootstrap, I get an error.

当我尝试从 提供文件时static/bootstrap,出现错误。

 <link rel=stylesheet type=text/css href="{{ url_for('static/bootstrap', filename='bootstrap.min.css') }}">

I can reference files that aren't in subfolders with this, which works.

我可以引用不在子文件夹中的文件,这很有效。

 <link rel=stylesheet type=text/css href="{{ url_for('static', filename='bootstrap.min.css') }}">

What is the correct way to reference static files with url_for? How do I use url_forto generate urls to static files at any level?

引用静态文件的正确方法是什么url_for?我如何使用url_for生成任何级别的静态文件的 url?

采纳答案by tbicr

You have by default the staticendpointfor static files. Also Flaskapplication has the following arguments:

默认情况下,您拥有静态文件的static端点。还Flask应用有以下参数:

static_url_path: can be used to specify a different path for the static files on the web. Defaults to the name of the static_folderfolder.

static_url_path: 可用于为网络上的静态文件指定不同的路径。默认为static_folder文件夹的名称。

static_folder: the folder with static files that should be served at static_url_path. Defaults to the 'static' folder in the root path of the application.

static_folder:包含静态文件的文件夹,应在static_url_path. 默认为应用程序根路径中的“静态”文件夹。

It means that the filenameargument will take a relative path to your file in static_folderand convert it to a relative path combined with static_url_default:

这意味着该filename参数将采用您的文件的相对路径static_folder,并将其转换为与static_url_default以下组合的相对路径:

url_for('static', filename='path/to/file')

will convert the file path from static_folder/path/to/fileto the url path static_url_default/path/to/file.

将文件路径 fromstatic_folder/path/to/file转换为 url path static_url_default/path/to/file

So if you want to get files from the static/bootstrapfolder you use this code:

因此,如果您想从static/bootstrap文件夹中获取文件,请使用以下代码:

<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='bootstrap/bootstrap.min.css') }}">

Which will be converted to (using default settings):

将转换为(使用默认设置):

<link rel="stylesheet" type="text/css" href="static/bootstrap/bootstrap.min.css">

Also look at url_fordocumentation.

另请查看url_for文档

回答by Andrew Grow

In my case I had special instruction into nginx configuration file:

就我而言,我对 nginx 配置文件有特殊说明:

location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
            try_files $uri =404;
    }

All clients have received '404' because nginx nothing known about Flask.

所有客户端都收到了“404”,因为 nginx 对 Flask 一无所知。

I hope it help someone.

我希望它可以帮助某人。