Flask--修改默認的static文件夾的方法


修改的flask默認的static文件夾只需要在創建Flask實例的時候,把static_folder和static_url_path參數設置為空字符串即可。

 

app = Flask(__name__, static_folder='', static_url_path='')

訪問的時候用url_for函數,res文件夾和static文件夾同一級:

res文件夾和static文件夾同一級,文件結構如下:

/--
  --run.py
  /--static
  /--templates
     --index.html
  /--res
     --sheeta.jpg 
url_for('static', filename='res/sheeta.jpg')

run.py代碼如下

from flask import Flask, render_template

app = Flask(__name__, static_folder='', static_url_path='')

@app.route('/')
def test():
    return render_template('index.html')

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

index.html內容如下:

 

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
    <img src="{{ url_for('static', filename='res/sheeta.jpg') }} " width="400" height="400" alt="" title=""/>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM