G'day!Python中的KeyError
所以這是我的代碼:
print """\
<form method="post">
Please enter Viewer Type:<br />
<table>
"""
#Viewer Type
print "<tr><td>Viewer Type<select name=""ViewerType"">"
print """\
<option value="C">Crowd Funding
<option value="P">Premium
"""
#do it button
print """\
<input type="submit" value="OK" />
"""
print """\
</form>
</body>
<html>
"""
ViewerType=form['ViewerType'].value
而且,當我把它投放到瀏覽器,這是錯誤:
Traceback (most recent call last): File "/home/nandres/dbsys/mywork/James/mywork/ViewerForm.py", >line 42, in ViewerType=form['ViewerType'].value File "/usr/lib/python2.7/cgi.py", line 541, in >getitem raise KeyError, key KeyError: 'ViewerType'
和線路42是我的代碼的最後一行。
該錯誤實際上並沒有影響功能,並且一切正常,但我並不想讓它彈出。任何建議/見解將不勝感激。
順便說一句,我有這個在我的代碼的頂部:
import cgi
form = cgi.FieldStorage()
謝謝!
感謝隊友,我用你的代碼來獲得靈感,並讓它工作。 – 2014-10-02 10:39:51
非常好!謝謝你的觀點。 PS。我希望你的實際程序不提供缺少結束標記的HTML等:) – 2014-10-02 10:44:36