烧瓶:在开发中,我如何实现媒体资产的动态路由? [英] Flask: In development, how can I achieve dynamic routing for media assets?

查看:186
本文介绍了烧瓶:在开发中,我如何实现媒体资产的动态路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask开发一个简单的Web应用程序。

文件/文件夹/子文件夹/资产[.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 
    $ 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)
    pre>

    然后当你去 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 real path is coded in the filename.

    folder and subfolder 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 the static_folder 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.

    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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆