简体   繁体   中英

Use Flask to serve both static CSS and static video files

I set up a Flask server to render raw MP4 videos. I can play the videos stored outside my Flask project folder by setting app = Flask(__name__, static_folder=<video_folder>) . But this will cause Flask to fail to find the CSS files stored inside the project folder for the website. Any solution to solve both? 'static_folder' cannot be a list.

Your video folder should be inside the 'static' folder, and you should have your URLs to videos point to 'static/video/file.mov' etc. You can use url_for to make sure the hrefs are constructed properly. See https://flask.palletsprojects.com/en/1.1.x/tutorial/static/ and https://flask.palletsprojects.com/en/1.1.x/quickstart/#url-building

If you cannot move the files for some reason, you can use send_from_directory in your route to do what you're describing:

app.route('/uploads/<path:filename>')
def download_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                               filename, as_attachment=True)

See https://flask.palletsprojects.com/en/1.1.x/api/#flask.send_from_directory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM