john john
john john

Reputation: 127

TypeError: argument 1 must be string or read-only character buffer, not None

I am gertting values from HTML file and getting Getting TracebackError: I am not getting to what should i change ?

Traceback (most recent call last):
  File "test.py", line 33, in <module>
    get_data()
  File "test.py", line 25, in get_data
    f.write(form.getvalue('firstname'))
TypeError: argument 1 must be string or read-only character buffer, not None

My python Code:

# !/usr/bin/python
import cgi

def get_data():
    '''
    This function writes the HTML data into the file 

    '''

    print "Content- type : text/html\n"

    form = cgi.FieldStorage()

    #Fname = form.getvalue('firstname')
    #Lname = form.getvalue('lastname')
   #Age = form.getvalue('age')
   #Gender = form.getvalue('gender')

    f = open("abc.txt","w")

    f.write(form.getvalue('firstname'))
    f.write(form.getvalue('lastname'))
    f.write(form.getvalue('age'))
    f.write(form.getvalue('gender'))
    f.close()

#print "Hello ", Fname, Lname, Age, Gender

get_data()

I am an newbie. and unable to get the error. please help me with that

HTML file:

<html>
<head>
<title>INFORMATION</title>
</head>
<body>
 <form action = "/cgi-bin/test.py" method = "post">
FirstName:
<input type = "text" name = "firstname" /><br>
LastName:
<input type = "text" name = "lastname" /><br>
Age:
<input type = "text" name = "age" /><br>
Gender:
<input type="radio" name="gender" value="male" /> Male
<input type="radio" name="gender" value="female" /> Female

<input type = "submit" name = "submit "value = "SUBMIT">
<input type = "reset" name = "reset" value = "RESET">
 </form>
</body>
</html>

Upvotes: 4

Views: 13549

Answers (1)

Nafiul Islam
Nafiul Islam

Reputation: 82550

form.getvalue('firstname') is returning None, and not the string that you expect it to, thats why you're getting an error.

Upvotes: 1

Related Questions