Flask没有收到HTML POST的数据

day*_*mer 1 python flask

@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'))
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

当我从HTML页面提交数据时,我明白了

data : ImmutableMultiDict([])
Run Code Online (Sandbox Code Playgroud)

为什么缺少数据?

Nem*_*den 7

您缺少字段名称(这是其中的键ImmutableMultiDict,这就是提交表单时它似乎为空的原因).

更改

<input type="text" id="email" placeholder="Email">
Run Code Online (Sandbox Code Playgroud)

<input name="<whatever_email_name>" type="text" id="email" placeholder="Email">
Run Code Online (Sandbox Code Playgroud)

<input type="password" id="password" placeholder="Password">
Run Code Online (Sandbox Code Playgroud)

<input name="<whatever_password_name>" type="password" id="password" placeholder="Password">
Run Code Online (Sandbox Code Playgroud)