[Catalog-sig] Assistance with Python Classes
Graeme Matthew
graeme@sofcom.com.au
Wed, 18 Jul 2001 11:32:22 +1000
This is a multi-part message in MIME format.
------=_NextPart_000_002B_01C10F7D.4F7FF8C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi there can someone please assist me
I am a perl programmer learning python. I keep on getting an exception =
error:
NameError: global name 'cgi' is not defined
I have a class called WebManager below
class WebManager:
=20
import os
import cgi
def __init__(self):
cgiObj =3D cgi.cgi()
self.formdata =3D cgiObj.FieldStorage()
self.operatingSys =3D os.name
def getParam(self,name): =20
=20
if self.formdata.has_key(name):
value =3D self.formdata[name].value
else:
value =3D none
return value
=20
def getGenericInfo(self):
return self.operatingSys
Im calling it from a standard script as seen below (the formatting ie =
indentation was lost when pasting)
import WebManager
obj =3D WebManager.WebManager()
fname =3D obj.getParam("fname")
lname =3D obj.getParam("lname")
address =3D obj.getParam("address")
tel =3D obj.getParam("tel")
mob =3D obj.getParam("mob")
if (lname !=3D "Not Supplied") and (fname !=3D "Not Supplied"):
message =3D "Thanks " + fname + " " + lname + " your data has been =
submitted"
else:
message =3D "As a minimum you must supply your first and last names"
print "Content-Type: text/html"
print
print "<HTML><BODY>"
print "<script language=3Djavascript>"
print "function openRecordForPrinting() {"
print "var hwnd =3D =
window.open(\"/cgi-bin/start.py\",\"printRecord\",\"status=3Dno\");}"
print "</script>"
print message
print "<table width=3D\"40%\">"
print "<FORM NAME=3DTEST METHOD=3DPOST ACTION=3D/cgi-bin/start.py =
target=3D\"test\">"
print "<tr><td>First Name:</td><td><INPUT TYPE=3DTEXT NAME=3Dfname =
VALUE=3D\"" + fname + "\"></td></tr>"
print "<tr><td>Last Name:</td><td><INPUT TYPE=3DTEXT NAME=3Dlname =
VALUE=3D\"" + lname + "\"></td></tr>"
print "<tr><td>Address:</td><td><INPUT TYPE=3DTEXT NAME=3Daddress =
VALUE=3D\"" + address + "\"></td></tr>"
print "<tr><td>Telephone Number:</td><td><INPUT TYPE=3DTEXT NAME=3Dtel =
VALUE=3D\"" + tel + "\"></td></tr>"
print "<tr><td>Mobile Number:</td><td><INPUT TYPE=3DTEXT NAME=3Dmob =
VALUE=3D\"" + mob + "\"></td></tr>"
print "<tr><td> </td><td><INPUT TYPE=3DSUBMIT NAME=3DSUBMIT =
VALUE=3DSEND></td></tr>"
print "<tr><td><a HREF=3D\"javascript:openRecordForPrinting()\">Click =
Here</a></td></tr>" =20
print """
</FORM>
</table>
</BODY>
</HTML>
"""
Thanks a million !!! =20
------=_NextPart_000_002B_01C10F7D.4F7FF8C0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4522.1800" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DTahoma size=3D2>Hi there can someone please assist =
me</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>I am a perl programmer learning =
python. I keep on=20
getting an exception error:</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>NameError: global name 'cgi' is not=20
defined</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>I have a class called WebManager=20
below</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma color=3D#0000ff size=3D2>class=20
WebManager:<BR> <BR> import=20
os<BR> import cgi</FONT></DIV>
<DIV><FONT face=3DTahoma color=3D#0000ff size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma color=3D#0000ff =
size=3D2> def=20
__init__(self):</FONT></DIV>
<DIV><FONT face=3DTahoma color=3D#0000ff size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma color=3D#0000ff =
size=3D2> =20
cgiObj =3D =
cgi.cgi()<BR> =20
self.formdata =3D=20
cgiObj.FieldStorage()<BR> =
=20
self.operatingSys =3D os.name</FONT></DIV>
<DIV><FONT face=3DTahoma color=3D#0000ff size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2><FONT =
color=3D#0000ff> def=20
getParam(self,name): <BR> <BR> &nb=
sp; =20
if=20
self.formdata.has_key(name):<BR> =20
value =3D=20
self.formdata[name].value<BR> =20
else:<BR> value =
=3D=20
none<BR> =
return=20
value<BR> <BR> def=20
getGenericInfo(self):<BR> =
=20
return self.operatingSys</FONT></FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>Im calling it from a standard script =
as seen below=20
(the formatting ie indentation was lost when pasting)</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>import WebManager</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>obj =3D =
WebManager.WebManager()</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>fname =3D =
obj.getParam("fname")<BR>lname =3D=20
obj.getParam("lname")<BR>address =3D obj.getParam("address")<BR>tel =3D=20
obj.getParam("tel")<BR>mob =3D obj.getParam("mob")</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>if (lname !=3D "Not Supplied") and =
(fname !=3D "Not=20
Supplied"):<BR> message =3D "Thanks " + fname + " " + lname + " =
your data has=20
been submitted"<BR>else:<BR> message =3D "As a minimum you must =
supply your=20
first and last names"</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>print "Content-Type:=20
text/html"<BR>print</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>print =
"<HTML><BODY>"<BR>print=20
"<script language=3Djavascript>"<BR>print "function =
openRecordForPrinting()=20
{"<BR>print "var hwnd =3D=20
window.open(\"/cgi-bin/start.py\",\"printRecord\",\"status=3Dno\");}"<BR>=
print=20
"</script>"<BR>print message<BR>print "<table=20
width=3D\"40%\">"<BR>print "<FORM NAME=3DTEST METHOD=3DPOST=20
ACTION=3D/cgi-bin/start.py target=3D\"test\">"<BR>print=20
"<tr><td>First Name:</td><td><INPUT =
TYPE=3DTEXT=20
NAME=3Dfname VALUE=3D\"" + fname + =
"\"></td></tr>"<BR>print=20
"<tr><td>Last Name:</td><td><INPUT =
TYPE=3DTEXT=20
NAME=3Dlname VALUE=3D\"" + lname + =
"\"></td></tr>"<BR>print=20
"<tr><td>Address:</td><td><INPUT TYPE=3DTEXT=20
NAME=3Daddress VALUE=3D\"" + address + =
"\"></td></tr>"<BR>print=20
"<tr><td>Telephone Number:</td><td><INPUT =
TYPE=3DTEXT=20
NAME=3Dtel VALUE=3D\"" + tel + "\"></td></tr>"<BR>print=20
"<tr><td>Mobile Number:</td><td><INPUT =
TYPE=3DTEXT=20
NAME=3Dmob VALUE=3D\"" + mob + "\"></td></tr>"<BR>print=20
"<tr><td>&nbsp</td><td><INPUT =
TYPE=3DSUBMIT=20
NAME=3DSUBMIT VALUE=3DSEND></td></tr>"<BR>print=20
"<tr><td><a =
HREF=3D\"javascript:openRecordForPrinting()\">Click=20
Here</a></td></tr>" </FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>print=20
"""<BR></FORM><BR></table><BR></BODY><BR></HTML><=
BR>"""</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT> </DIV>
<DIV><FONT face=3DTahoma size=3D2>Thanks a million=20
!!! </FONT></DIV></BODY></HTML>
------=_NextPart_000_002B_01C10F7D.4F7FF8C0--