版权声明:FatPuffer https://blog.csdn.net/qq_42517220/article/details/88684035
项目结构:
.
├── hello.py
├── static
└── templates
hello.py文件内容:
# coding:utf-8
from flask import Flask
# 创建flask应用
app = Flask(__name__)
# 访问路由地址
@app.route('/')
def index():
return 'hello world'
if __name__ == '__main__':
# 启动flask程序
app.run()
说明:
- __name__ 代表当前模块名
flask
以这个模块所在的目录为总目录- 默认这个目录中的
static
为静态目录,templates
为模板目录
启动flask应用
python hello.py
浏览器访问
http://127.0.0.1:5000/
接下来我们在static
静态文件目录下创建index.html
文件,内如下
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>Title</title>
</head>
<body>
<h1>hello python flask</h1>
</body>
</html>
通过浏览器直接访问该静态文件
http://127.0.0.1:5000/static/index.html