Reputation: 3908
I have an HTML form that accepts an arbitrary number of books and their authors. I'm trying to group the corresponding books and authors so that it's easily parsed by the server when the user submits the form. I tried using the HTML fieldset, but I'm not certain if that's the right approach.
When I submit the form to the server there's no notion of the fieldset in Flask's request.form
Here's the HTML form:
<form action="/save/" method="POST">
<fieldset>
<input name="author" value="Mark Twain" class="author" type="text">
<input name="book" value="The Adventures of Tom Sawyer" class="book" type="text">
</fieldset>
<fieldset>
<input name="author" value="Henry David Thoreau" class="author" type="text">
<input name="book" value="Civil Disobedience" class="book" type="text">
</fieldset>
<!--More books and authors here possibly-->
<button type="submit">Save</button>
</form>
And here's what I see on the server when I log request.form
:
ImmutableMultiDict([('book', u'The Adventures of Tom Sawyer'), ('book', u'Civil Disobedience'), ('author', u'Mark Twain'), ('author', u'Henry David Thoreau')])
You can see that the data the server receives has no notion of the fieldset or grouping of any sort.
Is there a better way to group the individual books with their corresponding authors? I'd prefer to do all of this in HTML rather than involve jQuery/JSON/AJAX.
Upvotes: 4
Views: 2625
Reputation: 318798
No, unfortunately this is not possible. The only data that's submitted for a field is its name and value. And while the way Flask handles form data (the name does not affect the handling) is generally better than in PHP (where a name like author[some_id]
would create an array) this is one of the few cases you need to handle manually.
The easiest way would be using names like author-ID
and title-ID
and then iterate over the fields, extracting the ID and actual name from the key.
Upvotes: 3