I have several elements in a form using Flask WTF with this naming convention...
# req_app_qty
# req_app_note
# req_main_qty
# req_main_note
# req_side_qty
# req_side_note
# req_bread_qty
# req_bread_note
# continued...
I can access the form data manually like this...
print "Manually (qty): " , form.req_app_qty.data # works fine
print "Manually (note): " , form.req_app_note.data # works fine
But I am trying to access this form data in a more automated fashion...
categories = [ "app", "main", "side", "bread", "dessert", "bev", "uten", "cups", "misc"]
for x in categories:
field1 = "req_%s_qty.data" % x # create a string to represent the attributes
field2 = "req_%s_note.data" % x # create a string to represent the attributes
qty_rqst = form.field1.data # fails
rqst_note = form.field2.data # fails
# also tried
print "QTY=", getattr(form, field1) # fails
print "Note:", getattr(form, field2) # fails
I tried these methods above and they have failed...
First method the lines fail with an error stating that the form does not have an attribute 'field1' or 'field2'.
As for second method of accessing form data, the following lines fail with a error stating there is no attribute 'req_app_qty.data'
print "QTY=", getattr(form, field1) # fails
How can I create a string to access these form attributes?
qty_rqst = form.field1.data # fails
This doesn't work because you're trying to access the field field1
, which doesn't exist.
print "QTY=", getattr(form, field1) # fails
This doesn't work because you're trying to access the field req_X_qty.data
, which doesn't exist.
You need to access fields that exist, e.g.
print 'QTY=', getattr(form, 'req_app_qty').data