问题描述
我正在传递多个表单字段,这些字段是可选的,但需要与用户关联。 Python的cgi.FormDict和cgi.FieldStorage都消除了空白条目,因此项目被上移并与错误的用户相关联。
I am passing multiple form fields which are optional, but which need to be associated with a user. Python's cgi.FormDict and cgi.FieldStorage both eliminate blank entries, so items get shifted "up" and associated with the wrong user.
此问题通常通过复选框显示(我有),但是我也有文本字段。
This problem most often shows up with checkboxes (which I have), but I also have text fields.
简化的表单代码:
<input type="text" name="user" />
<input type="text" name="email" />
<input type="text" name="phone" />
<input type="checkbox" value="MailingList1" />
<input type="checkbox" value="MailingList2" />
<p>
<input type="text" name="user" />
<input type="text" name="email" />
<input type="text" name="phone" />
<input type="checkbox" value="MailingList1" />
<input type="checkbox" value="MailingList2" />
etc...
用户需要输入电子邮件或电话(或同时输入),但可以保留另一个空白。
Users are required enter EITHER email or phone (or both) but can leave the other blank.
现在说我已经输入了以下内容:
Now say I have input like this:
john_doe [email protected] (123) 555-1234 Y Y
jane_roe [email protected] Y
george_jetson [email protected] (321) 555-4321 Y
FormDict看起来像这样:
The FormDict looks something like this:
{
'username':['john_doe','jane_roe','george_jetson'],
'email':['[email protected]','[email protected]','[email protected]'],
'phone':['(123) 555-1234','(321) 555-4321'],
'MailingList1':['Y','Y'],
'MailingList2':['Y','Y']
}
我像这样遍历:
for i in range(len(myFormDict['username'])):
username = myFormDict['username'][i]
email = myFormDict['email'][i]
phone = myFormDict['phone'][i]
MailingList1 = myFormDict['MailingList1'][i]
MailingList2 = myFormDict['MailingList2'][i]
...Inserts into the database
之前问,是的,我确实有错误陷阱,以防止它从列表末尾以及所有列表中消失。代码可以正常工作,但是我的数据库最终看起来像这样:
Before you ask, Yes, I do have error traps to prevent it from running off the end of the lists and all. The code works fine, but my database ends up looking like this:
john_doe [email protected] (123) 555-1234 Y Y
jane_roe [email protected] (321) 555-4321 Y Y
george_jetson [email protected]
简(Jane)和乔治(George)会生我的气。
Jane and George are going to be pretty mad at me.
所以...我该如何保持空白,以便正确的电话号码和列表中的成员会与合适的用户排在一起?
So... how do I keep the blanks so the right phone numbers and list memberships line up with the right users?
我在网络上找到的所有答案都涉及GAE,Django,Tornado,Bottle等框架。
All the answers I've found searching the web involve a framework like GAE, Django, Tornado, Bottle, etc.
瓶的重量最轻,但我尝试过,它需要Python 2.5,并且卡在2.4上。
Bottle is lightest weight, but I tried it and it requires Python 2.5 and I'm stuck on 2.4.
如果这些框架可以做到,那就必须有可能。那么,如何仅使用标准库就可以正确地管理我的数据呢?
If these frameworks can do it, it has to be possible. So how can I manage my data properly with just the standard library?
谢谢。
推荐答案
查看下面的文档(即 keep_blank_values
参数):
Look at the doc below (namely, keep_blank_values
parameter):
>>> print cgi.FieldStorage.__init__.__doc__
Constructor. Read multipart/* until last part.
Arguments, all optional:
fp : file pointer; default: sys.stdin
(not used when the request method is GET)
headers : header dictionary-like object; default:
taken from environ as per CGI spec
outerboundary : terminating multipart boundary
(for internal use only)
environ : environment dictionary; default: os.environ
keep_blank_values: flag indicating whether blank values in
percent-encoded forms should be treated as blank strings.
A true value indicates that blanks should be retained as
blank strings. The default false value indicates that
blank values are to be ignored and treated as if they were
not included.
strict_parsing: flag indicating what to do with parsing errors.
If false (the default), errors are silently ignored.
If true, errors raise a ValueError exception.
这篇关于使用Python CGI Stdlib获取空表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!