Skip to main content
 首页 » 编程设计

python之Flask 没有收到 HTML POST 的数据

2025年05月04日14jackei

@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')) 

我的 HTML 是

  <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> 

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

data : ImmutableMultiDict([]) 

为什么数据丢失了?

请您参考如下方法:

您缺少字段名称(它们是 ImmutableMultiDict 的键,这就是提交表单时它显示为空的原因)。

改变

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

到

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

和

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

到

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