简体   繁体   中英

Unable to serve static file from flask server

I have a index.html file, which has the absolute path 'c:\\project\\web\\frontend\\index.html'

I am trying to return it using the following function

@webserver.route('/')
def home()
     return webserver.send_static_file(path)

I have verified that the path is correct by accessing it directly in the browser.

I have tried to replace '\\' with '/' without any luck.

It is running on a windows machine.

If you look at flask's documentation for send_static_file . You'll see that it says that it's used internally by flask framework to send a file to the browser. If you want to render an html, it's common to use render_template . You need to make sure that your index.html is in a folder called templates first. So I would do the following:

@webserver.route('/')
def home()
     return flask.render_template('index.html') 

I had to define the path to be the static_folder, when creating the flask object. Once I defined the folder to be static, the html page was served.

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