烧瓶:request.form.getlist将不返回任何条目 [英] Flask: request.form.getlist won't return any entries

查看:178
本文介绍了烧瓶:request.form.getlist将不返回任何条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我无法获得我在4周前工作的东西:/ 我有一个网站,我在其中通过jQuery添加了一个检查按钮,我需要Flask来读出是否被检查. 我举了一个简短的例子,但仍然没有用.

for some reason I can't get this thing I worked 4 weeks ago working :/ I have a site where I add a checkbutton via jQuery and I need Flask to read out if it's checked or not. I made a minmial example, but it still doesn not work.

此处提供代码:

post.htm

<html>
<body>
<form name="formular" action='http://127.0.0.1:5000/formtest' method="post">
<input type="checkbox" value="test1">
<INPUT type ="submit" id="send" value="Do it"/>
</form>
</body>
</html>

webserver.py

@app.route('/formtest', methods=['POST'])
def formtest():
    checklist = request.form.getlist('test1')
    if checklist:
        check = True
    else:
        check = False
    if check:
        return("checked")
    else:
        return("not checked")

它总是返回未检查",但我不明白为什么.清单始终是一个空清单.错误在哪里?

It always returns "not checked", but I don't get why. Checklist is always an empty list. Where's the error?

提前谢谢!

推荐答案

您的复选框没有name属性;它永远不会作为POST的一部分发送.给它一个name属性以及一个值:

Your checkbox has no name attribute; it'll never be sent as part of the POST. Give it a name attribute as well as a value:

<input type="checkbox" name="test1" value="true">

在此不要使用MultiDict.getlist();您可以使用 MultiDict.get() 进行默认和类型转换相反:

Don't use MultiDict.getlist() here; you can use MultiDict.get() with a default and type conversion instead:

@app.route('/formtest', methods=['POST'])
def formtest():
    check = request.form.get('test1', default=False, type=bool)
    return 'checked' if check else 'not checked'

这篇关于烧瓶:request.form.getlist将不返回任何条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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