我当前的工作目录为/home/mark/a/b
,并且我的映像位于/home/mark/a/b/c/static
中,即映像的绝对路径为/home/mark/a/b/c/static/image.gif
。
我正在使用python函数send_static_file
通过GET
请求从端点返回图像。
我的问题是,为什么我不需要指定图像的完整路径,实际上我需要做的就是指定图像文件名,它能够检索图像并发送它到浏览器? send_static_file
是否自动搜索静态文件夹?
app = Flask(__name__, static_url_path='/static')
return app.send_static_file('image.gif')
如果在卷曲端点时仅指定图像文件名称本身以外的内容,则会显示以下错误。
{
"message": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. You have requested this URI [/event/email/] but did you mean /event/email/ or /event ?"
}
我看到一个帖子(第一个答案)在某种程度上回答了我的问题Flask: How to serve static html?。但是我不确定他们说的是什么意思
static_url_path changes the URL static files are available at, not the location on the filesystem used to load the data from.
文件系统上的位置与URL静态文件之间的区别是什么?
答案 0 :(得分:0)
文件系统上的位置与URL静态文件之间的区别是什么?
URL路由不应与服务器文件系统上的路径一致。您可以在static_url_path
应用初始化时同时指定static_folder
和Flask
参数,用于以下目的:
static_url_path
-可用于为网络上的静态文件指定其他路径(来自Flask documentation的描述)。例如。您指定了一些网址前缀
伺服器(例如localhost:8000 / some_prefix / ...),因此您所有的伺服器路由都可以使用这个前置字元。在这种情况下,static_url_path
也应使用以下前缀指定: some_prefix / static static_folder
-带有静态文件的文件夹,应在static_url_path
提供。默认为应用程序根路径中的“静态”文件夹(来自Flask documentation的描述)。换句话说,这是服务器文件系统上带有静态文件的文件夹的路径。我的问题是,为什么我不需要指定图像的完整路径,实际上我需要做的就是指定图像文件名,它能够检索图像并发送它到浏览器?
因为Flask
将在static_folder
中搜索指定的文件,该文件是服务器文件系统上包含静态文件的文件夹的路径。