简体   繁体   中英

TypeError 'classobj' object has no attribute '__getitem__' in python

I'm just a beginner at python and I have no idea how to fix this. Please help.

The error:

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 script:

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>"

form = cgi.FieldStorage assigns the class FieldStorage itself to form . You want to assign an instance of FieldStorage instead:

form = cgi.FieldStorage()

You don't instantiate FieldStorage .

What you do: form = cgi.FieldStorage . This makes form equal to the class cgi.FieldStorage . The class knows nothing about your current request.

What you mean: form = cgi.FieldStorage() . This creates a new instance associated with the current request.

Invoking the [] operator in python calls the __getitem__(key) method of the object. It seems that in your case that method was not defined for the class of which form is an instance.

To fix this, you will need to define the method and its behavior in the relevant class.

For more information, see the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM