[英]get integer from html form
I want to get the integer value from HTML form and I used the following python code: 我想从HTML表单获取整数值,并使用了以下python代码:
form = cgi.FieldStorage()
if not form.has_key("id"):
error_pop("There is no id in this form","The format of the request is not correct")
id = form["id"].value (*)
And in the HTML file, I make the input type as number
: 然后在HTML文件中,将输入类型设为
number
:
id: <input type="number" name="id" /><br />
However, it seems that the id I get from line ( *
) is still a string. 但是,似乎我从第(
*
)行获得的ID仍然是字符串。
How can I convert it to integer? 如何将其转换为整数?
I tried use int(form["id"].value)
, but python gave me the following error: 我尝试使用
int(form["id"].value)
,但是python给了我以下错误:
<type 'exceptions.TypeError'>
: %d format: a number is required, not str args = ('%d format: a number is required, not str',) message = '%d format: a number is required, not str'<type 'exceptions.TypeError'>
:%d格式:需要一个数字,而不是str args =('%d格式:需要一个数字,不是str',)message ='%d格式:需要一个数字,不是str'
So, I gave up using int()
. 所以,我放弃了使用
int()
。
If I try to print the value before parsing it to int()
, then I will get the internal server error from the browser. 如果我尝试在将其解析为
int()
之前打印该值,那么我将从浏览器中获取内部服务器错误。 Actually I'm always getting this error if I change something in the python file. 实际上,如果我更改python文件中的内容,总是会收到此错误。 I can see the error from the error_log.log:
我可以从error_log.log中看到错误:
/usr/lib/python2.6/cgitb.py:173: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
value = pydoc.html.repr(getattr(evalue, name))
Actually if I grep today's time from error.log, then it cannot show the current error..although it really exits some error that happens several hours ago... 实际上,如果我从error.log中获取今天的时间,则它无法显示当前错误..尽管它确实退出了几个小时前发生的错误...
I found something new: only if things involves the "id" thing, the internal server error will appear. 我发现了一些新东西:仅当事物涉及“ id”事物时,才会出现内部服务器错误。 If I do something like
id = form["idid"].value
, then it will give the error: 如果我做类似
id = form["idid"].value
,那么它将给出错误:
<type 'exceptions.TypeError'>: int() argument must be a string or a number, not 'NoneType'
args = ("int() argument must be a string or a number, not 'NoneType'",)
message = "int() argument must be a string or a number, not 'NoneType'"
Any help is appreciated. 任何帮助表示赞赏。
I was having the same problem you were, not being able to convert values from a form to numbers. 我遇到了同样的问题,无法将值从表单转换为数字。 Here's another solution I adapted from this blog post .
这是我根据此博客文章改编的另一种解决方案。 I have some values returned from radio buttons sets named q1, q2, q3, etc., and convert them using this dictionary.
我从名为q1,q2,q3等的单选按钮集中返回了一些值,并使用此字典对其进行了转换。
str2num = {"1":1, "2":2, "3":3, "4":4, "5":5, "6":6}
val1 = str2num[ form.getvalue("q1") ]
val2 = str2num[ form.getvalue("q2") ]
val3 = str2num[ form.getvalue("q3") ]
The value of the form element "q1" was the string form of the digit, so this converts it to to the numerical form. 表单元素“ q1”的值是数字的字符串形式,因此将其转换为数字形式。 Like you,
int( form.getvalue("q1") )
just wasn't working. 像您一样,
int( form.getvalue("q1") )
不能正常工作。 I still don't know why this does. 我仍然不知道为什么会这样。
It seems the value of the element is form
, that you pass to int
is the error message returned from the http server: 似乎传递给
int
的元素的值是form
,是从http服务器返回的错误消息:
"%d format: a number is required, not str" “%d格式:需要数字,而不是str”
For the sake of argument can you try the same thing without restricting the id field to type="number", but type="text". 为了论证,您可以尝试相同的操作而不必将id字段限制为type =“ number”,而是type =“ text”。 My guess is that this will allow the conversion to integer.
我的猜测是,这将允许转换为整数。 And if you need to enforce the result to be integer, use something like
如果您需要将结果强制为整数,请使用类似
while not id:
try:
id = int(form["id"].value)
except TypeError:
error_pop("id should be number")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.