将数据从模板发布到烧瓶蓝图路线会产生空字符串

时间:2016-02-04 14:49:31

标签: python flask

我正在努力修改cookiecutter Flask应用。

我有一个内置于公共/平面模板中的表单,如下所示:

      <form class="form-inline" id="registerForm"  method="POST" action="/get_email/" role="form">
          <div class="form-group">
            <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter your email address">
          </div>
          <button type="submit" class="btn btn-warning btn-lg">submitMe!</button>
        </form>

我的公共蓝图包含:

blueprint = Blueprint('public', __name__, static_folder="../static")

@login_manager.user_loader
def load_user(id):
    return User.get_by_id(int(id))

@blueprint.route("/", methods=["GET"])
def flat():
    return render_template('public/flat.html')

@blueprint.route("/get_email/", methods=['GET', 'POST'])
def get_email():
    email = request.get_data()
    return email

当我通过仅提交电子邮件测试电子邮件注册表单(上面的html)时,在我的调试器中我得到:

email = ''

在firefox网络面板中,我看到:

POST /get_email/

400 BAD REQUEST

127.0.0.1:5000

192 B

127.0.0.1:5000


31ms
HeadersPostResponseHTMLCacheCookies
view source
Content-Length  
192
Content-Type    
text/html
Date    
Thu, 04 Feb 2016 17:58:42 GMT
Server  
Werkzeug/0.10.4 Python/2.7.5
Set-Cookie  
session=eyJfaWQiOnsiIGIiOiJPVEprT1RsaU5ESXpNamMzTmpjMk5ESmpORGs1WmpGaFlXRTRObVpsWWpJPSJ9fQ.CZUi0g.mHrFfzFfFsX9PIjQmeN-3GQ1c2Y
; HttpOnly; Path=/
view source
Accept  
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding 
gzip, deflate
Accept-Language 
en-US,en;q=0.5
Connection  
keep-alive
Cookie  
csrftoken=kvecASUkun0BdgnLvf87MsW2hiARhVhr; session=eyJfaWQiOnsiIGIiOiJPVEprT1RsaU5ESXpNamMzTmpjMk5ESmpORGs1WmpGaFlXRTRObVpsWWpJPSJ9fQ
.CZUizg.9ZEEVV-XujIY8RQdK1Wk9cwC90M
Host    
127.0.0.1:5000
Referer 
http://127.0.0.1:5000/
User-Agent  
Mozilla/5.0 (Windows NT 6.1; rv:43.0) Gecko/20100101 Firefox/43.0
Content-Length  
0
Content-Type    
application/x-www-form-urlencoded

为什么帖子不起作用?

1 个答案:

答案 0 :(得分:1)

使用格式key=value提交表单。为了使表单包含字段,它必须具有name属性才能用作密钥。

<input name="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter your email address">

您可以更新get_email端点以直接使用该值。

def get_email():
    email = request.form['email']