【问题标题】:TypeError: argument 1 must be string or read-only character buffer, not NoneTypeError:参数 1 必须是字符串或只读字符缓冲区,而不是无
【发布时间】:2013-10-25 10:06:00
【问题描述】:

我正在从 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>

【问题讨论】:

  • 这不是你的问题,但值得注意的是最好use the with statement处理文件。
  • 似乎firstname 字段为空或缺失。您能显示生成要提交的表单的 HTML 吗?
  • @Lattyware 他正在使用 Python 2.6(来自上一个问题),不能使用 with。
  • form.getvalue('firstname') 是 None。可能,它没有从FieldStorage 获得价值。
  • @GamesBrainiac with 如果您使用 from __future__ import with_statement,则在 2.5 中添加了上下文管理器,在 2.6 中默认情况下可用。

标签: python cgi


【解决方案1】:

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

【讨论】:

  • 好的。我正在填写表单点击提交按钮,没有任何反应
  • @johnjohn 那么这是一个单独的问题。问题是,你提出的问题表明你得到了一个None,现在我们不知道form.getValue()返回什么样的函数。
  • 但是当使用打印时,它在提交按钮上显示正常。写入文件时,显示错误
猜你喜欢
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
  • 2023-03-03
相关资源
最近更新 更多