2016-11-21 80 views
0

文件index.html是静态的,因此通过render_template()传递它将是浪费。如果我store index.html under static/ 和使用app = Flask(__name__),一切都很好。如何使用平面烧瓶目录层次结构?

但如果我点static_url_path=''到根,并保持index.html在同一个文件夹中Python应用程序,我得到

127.0.0.1 - - [21/Nov/2016 14:35:54] "GET/HTTP/1.1" 404 - 

的index.html

<!DOCTYPE html> 
<head></head> 
<body> 
    <h2>Hello, World!</h2> 
</body> 

的.py

from flask import Flask, current_app 
app = Flask(__name__, static_url_path='') 

@app.route('/') 
def hello_world(): 
    return current_app.send_static_file('index.html') 

if __name__=="__main__": 
    app.run() 

如何使用完全平坦的目录层次结构,将上面的两个文件保存在同一个目录中?

+0

我会鼓励你通过瓶提供静态页面,如果这就是你需要做的。 查看'static_folder',而不是'static_url_path'。 查看[此帖子](http://stackoverflow.com/a/20648053/3261863)对于'send_from_directory',它可能对你很有用,因为你不需要破坏你的静态设置来提供一次性服务,关闭页面。 –

回答

0

我认为你需要设置static_folder而不是status_url_folder并指定文件夹的绝对路径,而不仅仅是一个空白字符串。

您可以print(app.static_folder)

0

查看默认的静态文件夹路径,您可以只使用flask.send_file()

import flask 

app = flask.Flask(__name__) 


@app.route('/') 
def hello_world(): 
    return flask.send_file('index.html') 

if __name__ == '__main__': 
    app.run()