E
E
Enter_a_nickname2022-03-17 15:05:20
Python
Enter_a_nickname, 2022-03-17 15:05:20

Why do I get an error about the empty hidden field of form #1 (dynamic) if I save data from another form?

The project has a page with forms. One of the forms is dynamic (DogFormSet) , duplication is due to the js script.
All functions for saving data from forms are separated by names so that django understands which form needs to be saved.
Separate saving of the dynamic form (DogFormSet) works correctly and does not cause errors.
But when saving data from other forms, I get an error on the dynamic form (after refreshing the page, the form disappears and I get the following errors:
(Hidden field TOTAL_FORMS) Required field.
(Hidden field INITIAL_FORMS) Required field.)

Despite the errors, data from other forms are saved.

my views.py:

class dogaddview(TemplateView):
     template_name = 'main/dogsform.html'

     def get(self, request, *args, **kwargs):
            form2 = SpeedsForms()
            formset = DogFormSet(queryset=dog.objects.none())
            form4 = ColorForms()
            form5 = DogRequestsForm()

            datas = {
                   'form2': form2,
                   'DogFormSet': formset,
                   'form4': form4,
                   'form5': form5,
                }
            return render(request, 'main/dogsform.html', datas)

     def post(self, request, *args, **kwargs):
            form2 = SpeedsForms(request.POST)
            formset = DogFormSet(request.POST)
            form4 = ColorForms(request.POST)
            form5 = DogRequestsForm(request.POST)

            if 'speed' and form2.is_valid():
                    form2.save()

            if 'dogs' and formset.is_valid():
                     formset.save()

            if 'color' and form4.is_valid():
                     form4.save()

            if 'Req' and form5.is_valid():
                      form5.save()

            data = {
                     'form2': form2,
                     'formset': formset,
                     'form4': form4,
                     'form5': form5,
                     }

            return self.render_to_response(data)


my html:
{% block content %}
    <div class="features">
        <form method="post">
            {% csrf_token %}<br>
            {{ form4.as_p }}<br>
            <button class="btn btn-success" type="submit" name="color">Сохранить</button>
        </form>
        <form method="post">
            {% csrf_token %}<br>
            {{ form2.as_p }}
            <button class="btn btn-success" type="submit" name="speed">Сохранить</button>
        </form>
        <form id="form-container" method="post">
            {% csrf_token %}<br>
            {{DogFormSet.management_form}}
            <div class="DogForm">
            {{ formset.as_p }}<br>
            </div>
            <button id="add-form" type="button">Add Another dogs</button>
            <button class="btn btn-success" type="submit" name="dogs">Сохранить</button>
        </form>
        <form method="post">
            {% csrf_token %}<br>
            {{ form5.as_p }}<br>
            <button class="btn btn-success" type="submit" name="Req">Сохранить</button>
        </form>
    </div>

    <script>
        let DogForm= document.querySelectorAll(".DogForm")
        let container = document.querySelector("#form-container")
        let addButton = document.querySelector("#add-form")
        let totalForms = document.querySelector("#id_form-TOTAL_FORMS")

        let formNum = DogForm.length-1
        addButton.addEventListener('click', addForm)

        function addForm(e){
            e.preventDefault()

            let newForm = DogForm[0].cloneNode(true)
            let formRegex = RegExp(`form-(\\d){1}-`,'g')

            formNum++
            newForm.innerHTML = newForm.innerHTML.replace(formRegex, `form-${formNum}-`)
            container.insertBefore(newForm, addButton)

            totalForms.setAttribute('value', `${formNum+1}`)
        }
    </script>

{% endblock %}


How can I avoid this error when saving data from other forms?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question