[Tutor] (no subject)

Peter Otten __peter__ at web.de
Sun Apr 2 12:07:49 EDT 2017


Белякова Анастасия wrote:

> Hi there! How are you?
> I need an advise.
> 
> I need to get values from html form, but form.getvalues['query'] returns
> None. 

I took a quick look into the cgi module, and FieldStorage class doesn't seem 
to have a getvalues attribute. You should get an exception rather than a 
None value. For easier debugging make sure your script starts with the lines

#!/usr/bin/env python3
import cgitb
cgitb.enable()

> Incorporated in function and not. The aim is to get the user input
> after filling form and clicking submit button. Here is a piece of code:
> 
> form = cgi.FieldStorage()
> 
> A = ['Course_Name', 'Gender', 'Phone_No', 'Residential_Address',
> 'Student_LastName',
>      'Student_FirstName', 'Password']
> data_values = {}for i in dict(form).keys():
>     if i in A:
>         data_values[(str(i))] = "'" + dict(form)[str(i)].value + "'"
> 
> 
> 
> course, gender, phone, adress, last_name, first_name, password=
> [data_values[i] for i in sorted(data_values.keys())]
> 
> And this is a fragment of html:
> 
> <form action="/cgi-bin/Hello.py" name="StudentRegistrationForm"
> method="post" onsubmit="return ValidateForm()">
> 
> <input type="text" name="Student_LastName" size="40" maxlength="30"
> style="font-weight: 700"/></font></td>
> 
> What could be the reason? What can I do?

Remove as much as you can from your script. E. g., does the following script 
show the contents of the form when you replace your current Hello.py with 
it?

#!/usr/bin/env python3
import cgitb
cgitb.enable()

import cgi
import sys

sys.stdout.write("Content-type: text/html\r\n\r\n")

print("""<html><body>""")
try:
    form = cgi.FieldStorage()
    cgi.print_form(form)
except:
    cgi.print_exception()
print("""</body></html>""")

If it doesn't double-check your setup (is the script executable, is your 
server configured properly etc.); once the basics work put stuff back in 
until the code does what you want -- or breaks. Then you can at least point 
to the exact statement that causes the breakage.



More information about the Tutor mailing list