Flask--静态资源

静态资源

from flask import Flask, render_template

app = Flask(__name__, template_folder="templates", static_folder="static", static_url_path="/static")

# template_folder:指定HTML文件查找目录
# static_folder:指定静态资源存放目录
# static_url_path:HTML中静态资源的标志

# 装饰器形式
# @app.route("/index")
# def index():
#     return render_template("index.html")

# 非装饰器形式
def index():
    return render_template('index.html')
app.add_url_rule('/index', 'index', index)

if __name__ == '__main__':
    app.run()
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
## 引用静态资源的两种方法,推荐第二种
<img src="/static/01.jpg"/>
  
<img src="{{ url_for('static',filename='01.jpg')}}" />
</body>
</html>
posted @   忘川的彼岸  阅读(2275)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示