E
E
Enter_a_nickname2022-03-15 20:48:33
Django
Enter_a_nickname, 2022-03-15 20:48:33

Why doesn't the duplicate form save data?

The project has a page with forms. One of the forms needs to be made dynamic, I implemented this functionality using javascript. This form is added according to the script correctly, but the added form does not save the data in the database.
To implement a dynamic form, I used this article: https://www.brennantymrak.com/articles/django-dyna

... I have several forms processed on 1 page in 1 views at once, I corrected the code for my project.

my forms.py:

class ColorForms(ModelForm):
    class Meta:
        model = Color
        fields = ['temp', 'correct', 'top', 'bottom']
        widgets = {
            'temp': NumberInput(attrs={
                'class': 'form-control',
                'placeholder': 'temp'
            }),
            'correct': NullBooleanSelect(attrs={
                'class': 'form-control',
                'placeholder': 'correct'
            }),
            'top': NumberInput(attrs={
                'class': 'form-control',
                'placeholder': 'top'
            }),
            'bottom': NumberInput(attrs={
                'class': 'form-control',
                'placeholder': 'bottom'
            }),
        }

    class SpeedsForms(ModelForm):
    class Meta:
        model = Speed
        fields = ['start',  'end']
        widgets = {
            "start": TimeInput(attrs={
                'class': 'form-control',
                'placeholder': "start"
            }),
            "end": TimeInput(attrs={
                'class': 'form-control',
                'placeholder': "end"
            })
        }

    DogFormSet = modelformset_factory(
    Dog, fields=('name', 'number', 'Speed', 'DataTime'), extra=1
)

    class DogRequestsForm(ModelForm):
    class Meta:
        model = DogRequest
        fields = ['id_name', 'Color', 'Dog']
        widgets = {
            'id_name': NumberInput(attrs={
                'class': 'form-control',
                'placeholder': 'id_name'
            }),
            'Color': SelectMultiple(attrs={
                'class': 'form-control',
                'placeholder': 'Color'
            }),
            'Dog': SelectMultiple(attrs={
                'class': 'form-control',
                'placeholder': 'Dog'
            }),
        }


my views.py:
from .forms import DogFormSet 

    def dog(request):
    formset = DogFormSet (queryset=dog.objects.none())

    if request.method == 'POST' and 'speed' in request.POST:
        form2 = SpeedsForms(request.POST)
        if form2.is_valid():
            form2.save()

    if request.method == 'POST' and 'dogs' in request.POST:
        formset = DogFormSet(request.POST)
        if formset.is_valid():
            formset.save()

    if request.method == 'POST' and 'color' in request.POST:
        form4 = ColorForms(request.POST)
        if form4.is_valid():
            form4.save()

    if request.method == 'POST' and 'Req' in request.POST:
        form5 = DogRequestsForm(request.POST)
        if form5.is_valid():
            form5.save()

    form2 = SpeedsForms()
    form4 = ColorForms()
    form5 = DogRequestsForm()
    data = {
        'form2': form2,
        'formset': formset,
        'form4': form4,
        'form5': form5,
    }

    return render(request, 'main/dogsform.html', data)


my html with duplication script:
{% 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 %}


In the article in the html file, the form is processed differently, but if I do as indicated in the article, the form is not displayed. The version of the code according to the article for me is this:
<form id="form-container" method="POST">
            {% csrf_token %}
            {{DogFormSet.management_form}}
            {% for form in DogFormSet %}
            <div class="DogForm">
            {{form.as_p}}
            </div>
            {% endfor %}
            <button id="add-form" type="button">Add Another dogs</button>
            <button class="btn btn-success" type="submit" name="dogs">Сохранить</button><br>
        </form>


What needs to be fixed? Are my views correct? Why is the data from the duplicate form not saved?

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