TypeError' classobj'对象没有属性' __ getitem __'在python中

时间:2014-08-01 00:26:50

标签: python

我只是python的初学者,我不知道如何解决这个问题。请帮忙。

错误:

Traceback (most recent call last):
  File "C:\Users\Priscilla\Desktop\CMPT Assn #3\page.py", line 17, in <module>
    print "<p>Customer Name:", form["custName"].value, "</p>"
TypeError: 'classobj' object has no attribute '__getitem__'

Python脚本:

import cgi
form = cgi.FieldStorage

# print HTTP/HTML header stuff
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>Order Form</title>
</head><body>
"""

# print HTML body using form data
print "<h1>Kintoro Japanese Bar &amp; Restaurant</h1>"
print "<h2>Customer Reciept</h2>"
print "<p>Customer Name:", form["custName"].value, "</p>"
print "<p>Customer Email Address:", form["custEmail"].value, "</p>"
print "<h2>Customer Address:</h2>"
print "<p>Street:", form["custAdd"].value, "</p>"
print "<p>City:", form["custCity"].value, "</p>"
print "<p>Province:", form["custProv"].value, "</p>"
print "<p>Postal Code:", form["custPostal"].value, "</p>"
print "<h2>Payment Information:</h2>"
print "<p>Card Type:", form["type1"].value, "</p>"
print "<p>Card Number: XXXX-XXXX-XXXX-", form["four4"].value, "</p>"
print "<p>Expiry Date:", form["expDate"].value, "</p>"

3 个答案:

答案 0 :(得分:2)

form = cgi.FieldStorage将班级FieldStorage分配给form。您想要分配FieldStorage的实例:

form = cgi.FieldStorage()

答案 1 :(得分:1)

您不会实例化FieldStorage

你做了什么:form = cgi.FieldStorage。这使form等于类cgi.FieldStorage。班级对你当前的要求一无所知。

你的意思是:form = cgi.FieldStorage()。这将创建与当前请求关联的新实例。

答案 2 :(得分:0)

在python中调用[]运算符会调用该对象的__getitem__(key)方法。在您的情况下,似乎没有为form是实例的类定义方法。

要解决此问题,您需要在相关类中定义方法及其行为。

有关详细信息,请参阅documentation