1、使用模板:
@app.route('/')def home():return render_template("homepage.html")#homepage.html在templates文件夾下
2、使用 send_from_directory
root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "html")#html是個文件夾@app.route('/')def home():return send_from_directory(root, "homepage.html")#homepage.html在html文件夾下
3、使用 app.send_static_file
app = Flask(__name__,static_url_path='')#修改靜態(tài)文件夾的目錄@app.route('/')def home():return app.send_static_file('homepage.html')#homepage.html在static文件夾下
4、flask 調(diào)用 css文件
app = Flask(__name__,static_url_path='')
@app.route('/')
def home():
return app.send_static_file('html/homepage.html')
<link rel="stylesheet" type="text/css" href="../css/homepagestyle.css">#html里面引用css
文件夾結(jié)構(gòu)目錄
注意:css文件必須在靜態(tài)文件夾下,否則css調(diào)用不了
以下內(nèi)容來自
https://www.cnblogs.com/shengulong/p/7106166.html 只為記錄下
flask中的g、add_url_rule、send_from_directory、static_url_path、static_folder的用法
一:Flask中的g對象
Flask中的g對象是個很好的東西,主要用于在一個請求的過程中共享數(shù)據(jù)??梢噪S意給g對象添加屬性來保存數(shù)據(jù),非常的方便,下面的代碼是一個使用g對象的例子。下面的這個例子會使用random隨機產(chǎn)生一個0~9的整數(shù),并使用g.x保存并記錄debug日志:
# encoding=utf-8from flask import Flaskfrom flask import gimport random app = Flask(__name__) @app.before_requestdef set_on_g_object(): x = random.randint(0,9) app.logger.debug('before request g.x is {x}'.format(x=x)) g.x = x @app.route("/")def test(): g.x=1000 return str(g.x) @app.after_requestdef get_on_g_object(response): app.logger.debug('after request g.x is{g.x}'.format(g=g)) return response
二:Flask中靜態(tài)文件的處理
1.add_url_rule的用法
Flask中提供了url_for來實現(xiàn)創(chuàng)建url,只是生成一個url。在前面的博文中談?wù)撨^如果要生成一個css樣式的靜態(tài)文件的url需要使用url_for('static',filename='style.css')來創(chuàng)建相應(yīng)的url。但是如果我有一個目錄attachment的目錄存放一些文件的話是沒法通過url_for來生成的,默認(rèn)url_for只可以為static和一些view_func建立url如果要想通過url_for為attachment來添加url就必須添加一個add_url_rule。
# encoding=utf-8from flask import Flaskfrom flask import gfrom flask import send_from_directoryfrom flask import url_forimport random app = Flask(__name__) @app.route("/")def test(): return "url創(chuàng)建方式一" def hello(): return "url創(chuàng)建方式二" app.add_url_rule("/index/",endpoint="hello",view_func=hello) @app.route('/url1')def Create_url1(): return url_for('static',filename="style.css") app.add_url_rule('/attachment/<path:filename>',endpoint='attachment',build_only=True)@app.route('/url2')def Create_url2(): return url_for('attachment',filename="upload.txt")
2.send_from_directory的用法
send_from_directory主要用于下載文件:
下面是一個文件的下載實例
# encoding=utf-8from flask import Flaskfrom flask import gfrom flask import send_from_directoryfrom flask import url_forimport os.path app = Flask(__name__)dirpath = os.path.join(app.root_path,'upload')@app.route("/download/<path:filename>")def downloader(filename): return send_from_directory(dirpath,filename,as_attachment=True)
首選在application下建立一個upload目錄,構(gòu)造upload目錄的絕對路徑。
然后通過瀏覽器輸入指定文件的文件名來下載。
3.static_url_path和static_folder的用法
static_url_path主要用于改變url的path的,靜態(tài)文件放在static下面,所以正常情況url是static/filename ,但是可以通過static_url_path來改變這個url
static_folder主要是用來改變url的目錄的,默認(rèn)是static,可以通過這個變量來改變靜態(tài)文件目錄。
# encoding=utf-8from flask import Flaskfrom flask import gfrom flask import send_from_directoryfrom flask import url_forimport os.path app = Flask(__name__,static_url_path="/test") @app.route("/")def static_create(): return url_for('static',filename='style.css')
4.靜態(tài)頁面緩存和文件索引
SEND_FILE_MAX_AGE_DEFAULT 這個變量用于配置靜態(tài)文件緩存的時間,F(xiàn)lask默認(rèn)緩存時間是12hours
例如: app.comfig['SEND_FILE_MAX_AGE_DEFAULT']=2592000 將其緩存時間改為了30天。
Flask不能實現(xiàn)文件索引的功能,也就是無法列出文件名,這個需要web server(Nginx 或 Apache)來實現(xiàn)。
5、session 也是一個 request context 的變量,但它把數(shù)據(jù)保存到了 cookie 中并發(fā)送到了客戶端,客戶端再次請求的時候又帶上了cookie