版权声明:尊重他人劳动成果,转载请注明出处 https://blog.csdn.net/qq_41432935/article/details/81744650
静态文件
静态文件通常包含CSS和JavaScript文件,开发期间,Flask只需在包或模块旁边创建一个static文件夹,它就可以在/static应用程序中找到。
注意:生产环境中,Flask关闭调试模式后加载不了静态资源,应通过服务器正向代理静态资源
静态文件加载 {{ url_for(‘static’, filename=”静态文件路径”) }}
如: url_for(‘static’, filename=’style.css’)
该文件必须存储在文件系统中static/style.css。
渲染模板
通过使用render_template() 方法来渲染模板,示例如下:
from flask import render_template
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html')
Flask将在templates文件夹中查找模板。因此,如果您的应用程序是一个模块,那么该文件夹就在该模块的旁边,如果它是一个包,它实际上在您的包中:
案例1:一个模块:
/application.py
/templates
/hello.html
案例2:一个包:
/application
/__init__.py
/templates
/hello.html