Flask: How to serve static html?

前端 未结 3 735
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 05:35

I am trying to serve a static html file, but returns a 500 error (a copy of editor.html is on .py and templates directory) This is all I have tried:

from fla         


        
3条回答
  •  情歌与酒
    2020-12-06 05:54

    send_from_directory and send_file need to be imported from flask.

    Your code sample would work if you do the following:

    from flask import Flask, send_from_directory
    app = Flask(__name__, static_url_path='/templates')
    
    @app.route('/')
    def hello_world():
        return send_from_directory('templates', 'editor.html')
    

    However, remember if this file loads other files e.g. javascript, css, etc. you would have to define routes for them too.

    Also, as far as I understand, this is not the recommended method on production because it's slow.

提交回复
热议问题