模板渲染
使用pycharm创建Flask项目
默认结构
- static (存放静态资源)
- templates (存放模板文件)
- renderFlask.py
在static 中创建css文件夹,在/static/css/中创建hello_css.css文件
vi hello_css.css
h2{
color:green;
}
p{
color:red;
}
运行:python renderFlask.py
访问:http://127.0.0.1:8000/static/css/hello_css.css
可直接看到之前写的css代码
在template中创建hello.html文件
vi hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
<link rel="stylesheet" href="/static/css/hello_css.css">
</head>
<body>
<h2>Hello</h2>
<p>World</p>
</body>
</html>
并修改renderFlask.py文件返回模板渲染
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/hello/')
def hello_world():
return render_template("hello.html")
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8000)
访问: “http://127.0.0.1:8000/hello/” 即可看到效果
源码地址:https://github.com/565785929/StudyFlask/tree/master/d03