如何使用Python的cgi模块正确获取formdata?

时间:2016-04-08 19:57:36

标签: python

我正在尝试从表单数据创建日期,但在运行模块时它会一直出现错误。错误是:

Traceback (most recent call last):
  File "/Users/Andrew/Desktop/weekday.py", line 19, in <module>
    weekday = calendar.weekday(year, month, day)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/calendar.py", line 113, in weekday
    return datetime.date(year, month, day).weekday()
TypeError: an integer is required

这是我的代码:

import cgi
import calendar

form = cgi.FieldStorage()

print """Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head>
<title>Weekday</title>
</head><body>
"""

year= form.getvalue("year")
month= form.getvalue("month")
day= form.getvalue("day")

weekday = calendar.weekday(year, month, day) 

if weekday == 0: 
   print "<p> That's a Monday </p>"

1 个答案:

答案 0 :(得分:0)

尝试在表单值上调用int(),如下所示:

year= int(form.getvalue("year"))
month= int(form.getvalue("month"))
day= int(form.getvalue("day"))