Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Python (CGI) question 1

Status
Not open for further replies.

gandalf458

IS-IT--Management
Jul 23, 2015
24
IT
Hi. I'm new to Python, trying to teach myself. I have the following script which works fine but I'm wondering if there are better ways of doing some things.

Code:
print("Content-type:text/html\r\n")

import cgi, cgitb

form = cgi.FieldStorage()

# Get data from form fields
# text fields
tname = form.getvalue('tname')
tdate = form.getvalue('tdate')

# textarea
if form.getvalue('comments'):
   comments = form.getvalue('comments')
else:
   comments = "None"

# checkbox
if form.getvalue('certificate'):
   checkbox1 = "yes"
else:
   checkbox1 = "no"

if form.getvalue('alumni'):
   checkbox2 = "yes"
else:
   checkbox2 = "no"

# radio button
if form.getvalue('grade'):
   grade = form.getvalue('grade')
else:
   grade = "Not set"

# dropdown
if form.getvalue('subject'):
   subject = form.getvalue('subject')
else:
   subject = "Not entered"

aoran = "a "
if (grade == "ordinary") or (grade == "upper second"):
   aoran = "an "

if ( grade != "ordinary" ):
   grade = grade + ' class honours'

print("""<!doctype html>
<html lang="en-gb">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Certificate</title>
</head>
<body>
<style>
body {
  font: .92em Verdana, Arial, Helvetica, sans-serif;
  text-align: center;
}
div {
  width: 20em;
  margin: 2em auto;
  padding: 2em;
  border: 1px solid black;
}
</style>
<div>""")
print("<h1>Certificate</h1>")
print("<h2>This is to certify that</h2>")
print("<h3> %s </h3>" % (tname))
print("<p>has been awarded</p>")
print("<p> %s %s degree</p>" % (aoran, grade))
print("<p>in %s </p>" % (subject))
print("<p>Date: %s </p>" % (tdate))
print("<p>Comments: %s </p>" % (comments))
print("<p>--- for official use ---<br>certificate: %s; alumni: %s </p>" % (checkbox1, checkbox2))
print("""</div>
</body>
</html>""")

The page that calls this script is at if that helps.

Thanks

I'm not a number, I'm a free man
 
Hi

I see nobody more qualified answered this, so I will enumerate some minor things I would write differently. Absolutely no intention to call my code better, is just a more compact coding style I find more readable.
Python:
[b]if[/b] form[teal].[/teal][COLOR=orange]getvalue[/color][teal]([/teal][i][green]'comments'[/green][/i][teal]):[/teal]
   comments [teal]=[/teal] form[teal].[/teal][COLOR=orange]getvalue[/color][teal]([/teal][i][green]'comments'[/green][/i][teal])[/teal]
[b]else[/b][teal]:[/teal]
   comments [teal]=[/teal] [i][green]"None"[/green][/i]

[gray]#   │[/gray]
[gray]#   ⋁[/gray]

comments [teal]=[/teal] form[teal].[/teal][COLOR=orange]getvalue[/color][teal]([/teal][i][green]'comments'[/green][/i][teal],[/teal] [i][green]'None'[/green][/i][teal])[/teal]
Python:
[b]if[/b] form[teal].[/teal][COLOR=orange]getvalue[/color][teal]([/teal][i][green]'certificate'[/green][/i][teal]):[/teal]
   checkbox1 [teal]=[/teal] [i][green]"yes"[/green][/i]
[b]else[/b][teal]:[/teal]
   checkbox1 [teal]=[/teal] [i][green]"no"[/green][/i]

[gray]#   │[/gray]
[gray]#   ⋁[/gray]

checkbox1 [teal]= [[/teal][i][green]'yes'[/green][/i][teal],[/teal] [i][green]'no'[/green][/i][teal]][[/teal][b]not[/b] form[teal].[/teal][COLOR=orange]getvalue[/color][teal]([/teal][i][green]'certificate'[/green][/i][teal])][/teal]
Python:
aoran [teal]=[/teal] [i][green]"a "[/green][/i]
[b]if[/b] [teal]([/teal]grade [teal]==[/teal] [i][green]"ordinary"[/green][/i][teal])[/teal] [b]or[/b] [teal]([/teal]grade [teal]==[/teal] [i][green]"upper second"[/green][/i][teal]):[/teal]
   aoran [teal]=[/teal] [i][green]"an "[/green][/i]

[gray]#   │[/gray]
[gray]#   ⋁[/gray]

aoran [teal]= [[/teal][i][green]'a'[/green][/i][teal],[/teal] [i][green]'an'[/green][/i][teal]][[/teal]grade[teal][[/teal][purple]0[/purple][teal]][/teal] [b]in[/b] [i][green]'aeiou'[/green][/i][teal]][/teal]


Feherke.
feherke.ga
 
Hi Feheke

That's just the kind of feedback I was hoping for. Many thanks

I'm not a number, I'm a free man
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top