烧瓶:request.form.getlist将不返回任何条目

编程入门 行业动态 更新时间:2024-10-21 05:33:29
本文介绍了烧瓶: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='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将不返回任何条目

本文发布于:2023-11-06 13:47:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563863.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:烧瓶   条目   将不   form   request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!