Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting data iterating over wtform fields

I've got a form, and number of dynamically adding fields,

class EditBook(Form):
    title = TextField('title', validators = [Required()])
    authors = FieldList(TextField())

that's how I append them

$('form').append('<input type="text" class="span4" id="authors-' + FieldCount +'" name="authors-' + FieldCount +'" placeholder="author ' + FieldCount +'">');

I want to get data from theese inputs. how can I iterate over them, using python?

(or how can I send collected data with jquery to server? I'm new to js and jquery)

guess my inputs aren't connected with authors Fieldlist.

UPDATE

my inputs aren't connected with EditBook though I append them to it.

form.data will solve rest of the problem, when I attach my inputs to form. (now I just get keyerror, trying to access form.data['authors-1'])

now I try to add just a single authors field to copy it later. but it renders invisible, for unknown reason. in blank space should be input, similar to "author-1"

{{form.authors(class="span4", placeholder="author 1")}}

what should I add to code to render this field correctly?

like image 966
user3684055 Avatar asked Feb 03 '26 04:02

user3684055


1 Answers

The WTForms process_formdata method will pull these out of the form submission and store them in the data attribute. Below is what your access code will look like. Your authors list should be stored in an iterable that can be accessed with the authors key.

from collections import namedtuple
from wtforms.validators import Required
from wtforms import Form
from wtforms import TextField
from wtforms import FieldList

from webob.multidict import MultiDict

class EditBook(Form):
    title = TextField('title', validators = [Required()])
    authors = FieldList(TextField())

Book = namedtuple('Book', ['title', 'authors'])
b1 = Book('Title1', ['author1', 'author2'])

# drop them in a dictionary 
data_in={'title':b1.title, 'authors':b1.authors}

# Build form and print
form = EditBook(data=MultiDict(data_in))

# lets get some data
print form.data
print form.data['title']
print form.data['authors']
like image 69
nsfyn55 Avatar answered Feb 04 '26 19:02

nsfyn55