Python个人学习笔记(三)

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值