Flask不接收HTML POST的数据 [英] Flask receiving no data for HTML POST

查看:167
本文介绍了Flask不接收HTML POST的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @ app.route('/',methods = ['GET','POST'])
def login():
request.method =='GET':
如果session中有'USER_TOKEN':
返回make_response(render_template('index.html'))
返回make_response(render_template('login.html'))
if request.method =='POST':
print'data:',request.form
return make_response(render_template('index.html'))

$ HTML



$ b

 < form class =form-horizo​​ntalaction =/method =POST> 
< div class =control-group>
< label class =control-labelfor =email>电子邮件< / label>
< div class =controls>
< input type =textid =emailplaceholder =Email>
< / div>
< / div>
< div class =control-group>
< label class =control-labelfor =password>密码< / label>
< div class =controls>
< input type =passwordid =passwordplaceholder =Password>
< / div>
< / div>
< div class =control-group>
< div class =controls>
< button type =submitclass =btn>登入< / button>
< / div>
< / div>
< / form>

当我从HTML页面提交数据时,我看到

  data:ImmutableMultiDict([])

数据丢失了?

解决方案

您缺少字段名称( ImmutableMultiDict

更改



<$ p $ < code>< input type =textid =emailplaceholder =Email>

 < input name =< whatever_email_name> type =textid =emailplaceholder =Email> 

 < input type =passwordid =passwordplaceholder =Password> 

 < input name =< whatever_password_name> type =passwordid =passwordplaceholder =Password> 


@app.route('/', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        if 'USER_TOKEN' in session:
            return make_response(render_template('index.html'))
        return make_response(render_template('login.html'))
    if request.method == 'POST':
        print 'data :', request.form
        return make_response(render_template('index.html'))

My HTML is

  <form class="form-horizontal" action="/" method="POST">
    <div class="control-group">
      <label class="control-label" for="email">Email</label>
      <div class="controls">
        <input type="text" id="email" placeholder="Email">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="password">Password</label>
      <div class="controls">
        <input type="password" id="password" placeholder="Password">
      </div>
    </div>
    <div class="control-group">
      <div class="controls">
        <button type="submit" class="btn">Sign In</button>
      </div>
    </div>
  </form>

When I submit data from HTML page, I see

data : ImmutableMultiDict([])

Why the data is missing?

解决方案

You are missing field names (which are keys for ImmutableMultiDict, that's why it appears to be empty when form gets submitted).

change

<input type="text" id="email" placeholder="Email">

to

<input name="<whatever_email_name>" type="text" id="email" placeholder="Email">

and

<input type="password" id="password" placeholder="Password">

to

<input name="<whatever_password_name>" type="password" id="password" placeholder="Password">

这篇关于Flask不接收HTML POST的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆