loops - Django: Save same formset multiple times -
this might sound stupid, have model_formset single model in views passed template. in template place formset inside loop:
<form action="generate/" method="post"> {% csrf_token %} {{ formset.management_form }} {% count in formset_count %} <p id="{{ count }}">{{ formset }}</p> {% endfor %} <div class="submitbutton" style="text-align: center;"> <input type="submit" value="submit"> </div> </form>
formset_count range of 1 5. have javascript automatically enters necessary values in formset , each formset different. submit , save 5 formsets simultaneously hitting submit button once.
in generate views have normal:
f = myformset(request.post) if f.is_valid(): f.save() return httpresponse("saved") else: return httpresponse(f.errors)
this saves last formset in loop.
is possible save 5 formsets (the same formset different values) @ once using modelformset_factory?
i think template needs little work. looks you're rendering entire formset 5 times, <p id="{{ count }}">{{ formset }}</p>
. can use django's built-in forloop.counter
sequential numbering.
<form action="generate/" method="post"> {% csrf_token %} {{ formset.management_form }} {% form in formset %} <p id="{{forloop.counter}}">{{ form }}</p> {% endfor %} <div class="submitbutton" style="text-align: center;"> <input type="submit" value="submit"> </div> </form>
your view code should fine, as per docs.
Comments
Post a Comment