Flask, Keep getting 404 serving static files using send_static_file

后端 未结 5 768
星月不相逢
星月不相逢 2020-12-28 13:46

I followed the instructions from How to serve static files in Flask, but still couldn\'t get it working.

Here\'s my project structure:

Project_path           


        
5条回答
  •  春和景丽
    2020-12-28 14:12

    Finally got it working. use flask.send_from_directory

    from flask import send_from_directory
    
    @app.route('/js/')
    def serve_static(filename):
        root_dir = os.path.dirname(os.getcwd())
        return send_from_directory(os.path.join(root_dir, 'static', 'js'), filename)
    

    It is now clear to me that flask really hate people putting app.py or in my case main.py into a subdirectory. Use send_static_file only if your static folder is what flask thinks to be, i.e. a folder with name static in the same directory with app.py.

提交回复
热议问题