Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get form field names and values in Python?

Tags:

python

I have a problem in web page, developed in python.

I have several fields (checkbox, textarea, etc) in form, and each field has some unique names.

I can save the value of known fields

i.e.

field_name = 'fl_textarea'    
field_value = form.getvalue(field_name)

But how I can get the value of unknown field names ? I also need to save field names into the variable.

When I have printed the contents of submitted form

form = cgi.FieldStorage()
print "<p>"+ str(form) +"</p>"

It looks like this:

FieldStorage(None, None, MiniFieldStorage('flatt2695', 'abc-xyz'), MiniFieldStorage('flatt2696', 'abc-123xyz'), MiniFieldStorage('flatt2697', 'onoff'), ...

So, how I can get these field names and values one by one into the variables ?

like image 809
Khokhar Avatar asked Nov 08 '13 14:11

Khokhar


1 Answers

its good you have explained everything in precise way.

Since you already have FieldStorage, so its easy for me to answer ;-)

If you want to get name of the form fields then print form.keys().

Other part is bit tricky. You can loop through the form.key() and write your own logic to retrieve the values on the base of keys.

I wrote a piece of compiled code here, but you can customize it according to your need.

variable = ""
value = ""
r = ""
for key in form.keys():
        variable = str(key)
        value = str(form.getvalue(variable))
        r += "<p>"+ variable +", "+ value +"</p>\n" 

fields = "<p>"+ str(r) +"</p>"        

Cheers,

like image 57
Mähboob Khökhar Avatar answered Oct 26 '22 09:10

Mähboob Khökhar