0%

Flask框架

前面啥也没有

目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
flask-demo/
├ run.py # 应用启动程序
├ config.py # 环境配置
├ requirements.txt # 列出应用程序依赖的所有Python包
├ tests/ # 测试代码包
│ ├ __init__.py
│ └ test_*.py # 测试用例
└ myapp/
├ admin/ # 蓝图目录
static/
│ ├ css/ # css文件目录
│ ├ img/ # 图片文件目录
│ └ js/ # js文件目录
├ templates/ # 模板文件目录
├ __init__.py
├ forms.py # 存放所有表单,如果多,将其变为一个包
├ models.py # 存放所有数据模型,如果多,将其变为一个包
└ views.py # 存放所有视图函数,如果多,将其变为一个包

示例:
IconFont网址下载图标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from flask import Flask,render_template,request
import datetime
app = Flask(__name__)

@app.route('/index')
def hello_world():
return 'Hello World!'

@app.route('/user/<name>')
def welcome(name):
return "welcome %s"%name

@app.route('/user/<int:id>')
def welcomeid(id):
return "welcome num %d"%id

@app.route("/")
def index2():
list = ["l1","l2","l3"]
time = datetime.date.today()
task = {"task":"clean","time":"3hours"}
return render_template("index.html",var = time,list = list, task = task)

#表单提交
@app.route('/register')
def register():
return render_template("register.html")

@app.route('/result',methods=['POST','GET'])
def result():
if request.method == 'POST':
result = request.form #表单字典
return render_template("result.html",result = result)

if __name__ == '__main__':
app.run(debug=True) #还需要在项目配置中启动Debug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
welcome.
Today is {{ var }}<br/>
on duty<br/>
{% for data in list %}
<li> {{ data }}</li>
{% endfor %}
task:s<br/>
<table border="1">
{% for key,value in task.items() %}
<tr>
<td>{{key}}</td>
<td>{{value}}</td>
</tr>
{% endfor %}
</table>

</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="{{ url_for('result') }}" method="post">
<p>name: <input type="text" name="name"></p>
<p>age: <input type="text" name="age"></p>
<p>sex: <input type="text" name="sex"></p>
<p><input type="submit" value="submit"></p>

</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
{% for key,value in result.items() %}
<tr>
<th>{{key}}</th>
<td>{{value}}</td>
</tr>
{% endfor %}
</table>
Result
</body>
</html>