Hello hello,
Imagine a form like this:
<form action="{% url 'somewhere' %}" method="post">
<ul>
<li>
<label for="mytext0"></label>
<input type="text" value="" name="mytext" id="mytext0">
<button type="button">Add a new text</button>
</li>
</ul>
</form>
When the user clicks the button Add a new text
, some javascript dynamically populate the form so it becomes:
<form action="{% url 'somewhere' %}" method="post">
<ul>
<li>
<label for="mytext0">My text</label>
<input type="text" value="" name="mytext" id="mytext0">
<button type="button">Add a new text</button>
</li>
<li>
<label for="mytext1">My text</label>
<input type="text" value="" name="mytext" id="mytext1">
<button type="button">Add a new text</button>
</li>
</ul>
</form>
… and so on.
Notice the text inputs have the same name
attribute.
In GET method, this would work and result in a query string similar to ?mytext=foo&mytext=bar
.
In POST method, this would result in something like:
-----------------------------44380802120381848453673825967
Content-Disposition: form-data; name="mytext"
bar
-----------------------------44380802120381848453673825967
Content-Disposition: form-data; name="mytext"
foo
-----------------------------44380802120381848453673825967--
Now how to handle this dynamically in django? [As far as I know, cleaned_data
does not support getlist()
for CharField
? (please tell me I’m wrong!) ]
- In the
clean()
method of the form - in the
form_valid
method of the view
Thanks!