python - FormSet saves the data of only one form -
when submitted forms (but on page filled id more 1 form) - formset saves data of 1 form, rest of data disappear...
my template:
<div id="data"> <form method="post" action="/lookup/" id="test_data">{% csrf_token %} {{ formset.management_form }} {% form in formset %} <section id="test_data_row"> {{ form }} </section> {% endfor %} </form> </div> <div class="bt"> <button type="submit" class="btn btn-default" id="submit_form" form="test_data">submit</button> <button type="button" class="btn btn-default" id="add" value="add row"/>add row</button> </div>
my forms.py:
class lookupform(forms.modelform): class meta: model = exclude = () lookupformset = formset_factory(lookupform, can_delete=true)
my model
class look(models.model): class meta(): db_table = 'lookup' id_device = models.charfield(max_length=75) phone_number = models.charfield(max_length=100) phone_number_country = models.charfield(max_length=1000) text = models.charfield(max_length=1000, default=none)
my views.py:
def manage_articles(request): lookupformset = modelformset_factory(model=look, exclude=()) if request.method == "post": formset = lookupformset( request.post, request.files, queryset=look.objects.none(), ) if formset.is_valid(): form in formset: form.save() return httpresponseredirect('/') else: formset = lookupformset(queryset=look.objects.none()) return render(request, 'req/lookup.html', {'formset': formset})
my js (js add new form):
document.getelementbyid('add').onclick = duplicate; var = 0; var original = document.getelementbyid('test_data'); function duplicate() { var clone = original.clonenode(true); // "deep" clone clone.id = "test_data" + ++i; // there can 1 element id original.parentnode.appendchild(clone); }
you cannot save formset contains multiple forms. suggest change code to:
if formset.is_valid(): form in formset: form.save() return httpresponseredirect('/')
see docs.
Comments
Post a Comment