TypeError:参数1必须是字符串或只读字符缓冲区,而不是None

时间:2013-10-25 10:06:01

标签: python cgi

我正在从HTML文件中获取值并获取 获取TracebackError:我没有得到我应该改变的内容?

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

我的python代码:

# !/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()

我是新手。并且无法得到错误。请帮帮我

HTML文件:

<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>

1 个答案:

答案 0 :(得分:0)

form.getvalue('firstname')正在返回None,而不是您期望的字符串,这就是您收到错误的原因。