flask之request(get post 请求在后端怎么取值)

后端的代码

from flask import Flask,render_template,request

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('06_index.html')

@app.route('/register',methods=['POST'])
def register():
    print(request.path)
    print(request.values)
    print(request.data)
    print(request.method)
    print(request.headers)
    print(request.host)
    print(request.full_path)
    print(request.url)
    print(request.form)
    print(request.args)
    name = request.form.get('name')
    pwd = request.form.get('pwd')
    hobby = request.form.getlist('hobby')#取多个值的话要用getlist
    return '注册成功'

@app.route('/register1',methods=['GET'])
def register1():
    print(request.path)
    print(request.values)
    print(request.data)
    print(request.method)
    print(request.headers)
    print(request.host)
    print(request.full_path)
    print(request.url)
    print(request.form)
    print(request.args)
    name = request.args.get('name')
    pwd = request.args.get('pwd')
    #pwd = request.values.get('pwd')
    hobby = request.values.getlist('hobby')#取多个值的话要用getlist
    #get方法获取数据可以用args 也可以用values
    return '注册成功'+ name




if __name__ == '__main__':
    app.run(debug=True)

前端的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>


<form action="/register1" method="get">
    <div>账号:<input type="text" name="name"></div>
    <div>密码:<input type="password" name="pwd"></div>
    <div>
        爱好:吃<input type="checkbox" name="hobby" value="吃">
        喝:<input type="checkbox" name="hobby" value="喝">
        玩:<input type="checkbox" name="hobby" value="玩">

    </div>
    <div><input type="submit" value="提交"></div>
</form>


<form action="/register" method="post">
{#一定要写明白,提交到哪个表单上面,请求方法是什么#}
    <div>账号:<input type="text" name="name"></div>
    <div>密码:<input type="password" name="pwd"></div>
    <div>        爱好:吃<input type="checkbox" name="hobby" value="吃">
        喝:<input type="checkbox" name="hobby" value="喝">
        玩:<input type="checkbox" name="hobby" value="玩">    </div>
    <div><input type="submit" value="提交"></div>
</form>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值