Flask获取Post请求的参数

Zss 发表于:

从前端Post提交的账号密码的表单需要在后端来接收,POST和GET的接收方法不一样

接收POST的方法为:request.form[‘传进来的参数’]    传进来的参数代表着这个标签的name属性的value

(1).request.form.get(‘username’)

(2).request.form[‘password’]

HTML:

<form  action="/" method="post">
    账号<input name="username">
    密码<input name="password">
   <button type="submit">确认</button>
</form>

Python:

@app.route('/',methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        name,password = request.form.get('username'),request.form['password']
        if  name== 'zxc' and password == 'zxc':
            print name ,password
            return 'login_pass'
        else:
            print name, password
            return 'login_fail'

mobile = request.form.get("mobile")

password = request.form.get("password", type=str, default=None)

password_repeat = request.form.get("password_repeat", type=str, default=None)

mobile_code = request.form.get("mobile_code", type=str, default=None)