flask框架——静态资源
py文件
使用 render_template 返回一个html文件
# 创建static目录,它应该和程序模块、templates在同一目录,层级。 from flask import Flask, render_template #创建Flask应用程序实例 app = Flask(__name__) #定义路由和视图函数,显示icon @app.route("/") def show_icon(): return render_template('static.html') #运行应用程序 if __name__=="__main__": app.run(port=5051,debug=True)
static.html文件

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <meta name="viewport" content="width=cdevice-width, initial-scale=1.0"/> <title>静态文件</title> <link rel="icon" href="{{url_for('static', filename='image/logo.png')}}"> <!-- 引入css演示--> <link rel="stylesheet" href="{{url_for('static', filename='style.css')}}"> </head> <body> <h1>静态文件</h1> <h2> <!-- url_for()显示图片,生成静态图片文件的完整URL: /static/image/gift.jpg --> <img alt="branch" class="pic" src="{{url_for('static', filename='image/gift.jpg')}}"> 测试一下 </h2> <div> <img alt="branch" class = "hogwarts" src="{{url_for('static', filename='image/write.png')}}"> </div> </body> </html> <script type="text/javascript"></script>