Getting empty form fields with Python cgi stdlib

后端 未结 3 1052
遇见更好的自我
遇见更好的自我 2021-01-26 05:44

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 i

相关标签:
3条回答
  • 2021-01-26 06:12

    I'm not sure if you can tweak cgi.FromDict to do what you want, but maybe a solution would be to make every input set uniqe like so:

    <input type="text" name="user" />
    <input type="text" name="email" />
    <input type="text" name="phone" />
    <input type="checkbox" value="MailingList1_1" />
    <input type="checkbox" value="MailingList2_1" />
    <p>
    <input type="text" name="user" />
    <input type="text" name="email" />
    <input type="text" name="phone" />
    <input type="checkbox" value="MailingList1_2" />
    <input type="checkbox" value="MailingList2_2" />
    

    Note thate MailingList1 and Mailinglist2 now hafe a suffix for the "entry block".

    You will have to add this at generation time of you Form and then adapt your processing accordingly by reading the checkboxes like so:

    lv_key = 'MailingList1_' + str(i)
    MailingList1 = myFormDict[lv_key]
    ...
    

    Regards,

    Martin

    0 讨论(0)
  • 2021-01-26 06:17

    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.
    
    0 讨论(0)
  • 2021-01-26 06:30

    Even if you're using wsgi based frameworks like django or bottle, you're using same <input> names for each field - cgi formdict does not eliminate empty fields, there are no empty fields in the first place. you should give different name attribute for each user.

    0 讨论(0)
提交回复
热议问题