2016-11-21 78 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()