烧瓶:在开发中,我如何实现媒体资产的动态路由? [英] Flask: In development, how can I achieve dynamic routing for media assets?
问题描述
在文件/文件夹/子文件夹/资产[.pdf | .jpg]中有静态媒体资源(照片,PDF等)
。但是我希望这些文件的URL看起来像这样: files / folder_subfolder_asset [.pdf | jpg]
。如您所见,真正的路径
被编码在文件名
中。
$ b对于每个图像,$ b
文件夹
和子文件夹
是不同的。有很多图像! 在生产上,在服务器上,我将处理 NGINX ,我怎么能在开发 ?注意:在开发上,我使用 Flask run
来测试应用程序。如果我需要使用不同的东西,请咨询。
您可以尝试使用(Application Objects或Blueprint Objects, $ b $ ul
/docs/0.12/api/rel =nofollow noreferrer> doc ): static_url_path
- 可以用来指定Web上静态文件的不同路径。默认是 static_folder
文件夹的名称。 static_folder
应该在 static_url_path
处提供静态文件。默认为应用程序根路径中的static文件夹。例如:
您可以将 static_foler
设置为静态文件目录: / files / folder / subfolder / asset
, static_url_path
到 / files / folder_subfolder_asset
:
from flask import烧瓶,请求,重定向,url_for
pre>
$ b app =烧瓶(__ name __,static_folder ='文件/文件夹/子文件夹/资产/',static_url_path ='/ files / (),
$ b @ app.route('/',methods = ['GET'])
def root():
return redirect(url_for('static', filename ='figure.jpeg'))
if __name__ =='__main__':
app.run(debug = True)
然后当你去
http://127.0.0.1:5000
时,它会重定向到http://127.0.0.1:5000/files/folder_subfolder_asset/figure.jpeg
,url
更改为定义的那个在sta tic_url_path
I am using Flask to develop a simple web application.
I have static media assets (photographs, PDFs etc) inside
files/folder/subfolder/asset[.pdf|.jpg]
. But I want the URL to the files to look like this:files/folder_subfolder_asset[.pdf|jpg]
. As you can see, the realpath
is coded in thefilename
.
folder
andsubfolder
are different for each image. There are a lot of images!On production, on the server, I will be handling the serving of the files with NGINX, how can I accomplish something similar on development?
Note: on development I'm using
Flask run
to test the application. Please advice if I need to use something different.解决方案You could try with (Application Objects or Blueprint Objects, doc) :
static_url_path
– can be used to specify a different path for the static files on the web. Defaults to the name of thestatic_folder
folder.static_folder
– the folder with static files that should be served atstatic_url_path
. Defaults to the 'static' folder in the root path of the application.
eg:
Your could set static_foler
to static file directory: /files/folder/subfolder/asset
, and static_url_path
to /files/folder_subfolder_asset
:
from flask import Flask, request, redirect, url_for
app = Flask(__name__,static_folder='files/folder/subfolder/asset/', static_url_path='/files/folder_subfolder_asset')
@app.route('/', methods=['GET'])
def root():
return redirect(url_for('static', filename='figure.jpeg'))
if __name__ == '__main__':
app.run(debug=True)
Then when you go to http://127.0.0.1:5000
, it will redirect to http://127.0.0.1:5000/files/folder_subfolder_asset/figure.jpeg
, the url
was change to the one defined in static_url_path
这篇关于烧瓶:在开发中,我如何实现媒体资产的动态路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!