由于某种原因,我无法获得我在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='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将不返回任何条目
发布评论