Skip to main content
 首页 » 编程设计

Python "classobj"错误

2025年05月04日17qlqwjy

我有这段代码,但我无法运行它,因为我得到了这个错误: “TypeError:'classobj' 对象不可订阅” 这是我的代码:

import cgi 
import customerlib 
 
form=cgi.FieldStorage 
 
history = customerlib.find(form["f_name"].value,form["l_name"].value) 
 
 
print "Content-type: text/html" 
print 
print """<html> 
  <head> 
    <title>Purchase history</title> 
  </head> 
  <body> 
    <h1>Purchase History</h1>""" 
 
print "<p>you have a purchase history of:" 
for i in history: "</p>" 
   print"""  <body> 
</html>""" 

我在这个文件旁边有 customerlib 文件。知道如何解决吗?

请您参考如下方法:

form=cgi.FieldStorage 

FieldStorage是一个类,不是一个对象。您需要实例化它以创建 FieldStorage对象:

form=cgi.FieldStorage() 

它在 form["f_name"] 上出错因为 form 当前是 FieldStorage 类的别名, 不是 FieldStorage 类型的对象.通过实例化它,它正在做您认为应该做的事情。

查看 cgi module documentation有关如何使用 CGI 模块的更多详细信息。