Flask框架
app.py
# Flask
from flask import Flask,render_template,request
import datetime
app = Flask(__name__)
# @app.route('/')
# def hello_world():
# return 'Hello World!'
# @app.route('/index') # 用户输入的内容index /指的是当前路径
# def hello_world():
# return '你好,Flask~'
# @app.route('/user/<name>') # 用户输入的内容index /指的是当前路径
# def hello(name):
# return '你好,Mr.%s~'%name
# @app.route('/user/<int:id>') # 用户输入的内容index /指的是当前路径
# def hello(id):
# return '您好,您是第%d位来宾~欢迎光临'%id
# @app.route("/") # 用户输入的内容index /指的是当前路径
# def hello():
# time = datetime.date.today()
# name = ["张三","李四","赵五"]
# task = {"任务":"值班","时长":"3小时"}
# return render_template("test.html",var = time,name = name,task = task)
#表单提交 注册页面
@app.route("/test/register") # 用户输入的内容index /指的是当前路径
def register():
return render_template("Test/register.html")
# 点击提交显示的内容 methods 默认为GET POST则需以下列表赋值声明
@app.route('/result',methods=['POST','GET']) # 用户输入的内容(路径):/result /指的是当前路径
def result():
if request.method == 'POST':
result = request.form
return render_template("Test/result.html", result = result)
if __name__ == '__main__':
app.run()
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
你好,现在是{{ var }},欢迎光临~<br/> {# <br/> 换行标签 {{ 一般变量 }} #}
今天的值班人员如下:<br/>
{% for data in name %}
<li>{{ data }}</li> {# <li> </li> 分点标签 #}
{% endfor %} {# html的for循环格式:{% for 遍历遍历 in 数据结构 %}...循环内容...{% endfor %} 循环结束 #}
今日任务:<br/>
<table border="1"> <!--<table> <table/> 表格-->
{% for key,value in task.items() %} <!--items迭代器,将 字典 转换成 列表[元组]--[(key,value),(key,value)...]-->
<tr> <!--<tr> <tr/> 表格中每一行-->
<td>{{ key }}</td>
<td>{{ value }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
{# <form action="http://localhost:5000/result" method="post">#}
{# <p>name: <input type="text" name = "姓名"></p>#}
{# <p>age: <input type="text" name = "年龄"></p>#}
{# <p>gender: <input type="text" name = "性别"></p>#}
{# <p>address: <input type="text" name = "地址"></p>#}
{# <p>tel: <input type="text" name = "联系方式"></p>#}
{# <p> <input type="submit" name = "提交"></p>#}
{# </form>#}
{##}
{#<!-- 反向路由 url_for('搜索路径名') 直接显示路径并调用相关函数 使html网址在外部以result显示 -->#}
<form action="{{ url_for('result') }}" method="post">
<p>name: <input type="text" name = "姓名"></p>
<p>age: <input type="text" name = "年龄"></p>
<p>gender: <input type="text" name = "性别"></p>
<p>address: <input type="text" name = "地址"></p>
<p>tel: <input type="text" name = "联系方式"></p>
<p> <input type="submit" name = "提交"></p>
</form>
</body>
</html>
register的跳转页 result.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
注册成功!
<table border="1"> <!--<table> <table/> 表格-->
{% for key,value in result.items() %} <!--items迭代器,将 字典 转换成 列表[元组]--[(key,value),(key,value)...]-->
<tr> <!--<tr> <tr/> 表格中每一行-->
<th>{{ key }}</th>
<td>{{ value }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>