mardi 20 mai 2014

Créer un dictionnaire avec un formulaire HTML dans Django - Stack Overflow


Is there any possible method to create a form with one or more elements like:


<form action="{% url "foo" %}" method="POST">
<div id="report_item">
<input type="text" name="dontknow">
<input type="date" name="dontknow">
</div>
<div id="report_item">
<input type="text" name="dontknow">
<input type="date" name="dontknow">
</div>
</form>

And to get a dictionary from this form as below:


{data:
{1: {
text: 'bla', date: '2014-03-02'
},
{2: {
text: 'second text', date: '2014-03-01'
}
}



Not quite, but you probably want to look at formsets. That gives you a series of repeated forms, which you can output on the template by just iterating through. Once you've validated the POST data if you really need a dictionary you can do something like:


data = {i: form.cleaned_data for i, form in enumerate(formset.forms)}


Is there any possible method to create a form with one or more elements like:


<form action="{% url "foo" %}" method="POST">
<div id="report_item">
<input type="text" name="dontknow">
<input type="date" name="dontknow">
</div>
<div id="report_item">
<input type="text" name="dontknow">
<input type="date" name="dontknow">
</div>
</form>

And to get a dictionary from this form as below:


{data:
{1: {
text: 'bla', date: '2014-03-02'
},
{2: {
text: 'second text', date: '2014-03-01'
}
}


Not quite, but you probably want to look at formsets. That gives you a series of repeated forms, which you can output on the template by just iterating through. Once you've validated the POST data if you really need a dictionary you can do something like:


data = {i: form.cleaned_data for i, form in enumerate(formset.forms)}

0 commentaires:

Enregistrer un commentaire