Как сохранить наборы форм в django
Я хочу добавить несколько форм при нажатии на кнопку и использую наборы форм для этого, но когда я пытаюсь сохранить формы, он не выдает ошибку, но и не сохраняет их.
my views.py:
def StepThreeView(request):
formSet = modelformset_factory(Club, fields=("name", "channel", "logo", "created_by"))
form = formSet(queryset=Example.objects.none())
if (request.method == "POST"):
form = formSet(request.POST)
if (form.is_valid()):
form.save()
context = {
'form': form
}
return render(request, 'on_boarding/step_three.html', context)
мой html:
<form method="POST" id="form">
{% csrf_token %}
{{ form.management_form }}
<div class="hidden" id="form-copy">
{{ form.empty_form.as_p }}
</div>
</form>
<button class="add-club" type="button" onclick="AddUser()">
Add user
</button>
мой javascript:
function AddUser() {
const formCopyTarget = document.getElementById("formCopyTarget");
const copyForm = document.getElementById("form-copy").cloneNode(true)
const totalNewForm = document.getElementById('id_form-TOTAL_FORMS')
const currentForms = document.getElementsByClassName("block")
let totalForms = currentForms.length
copyForm.setAttribute('class', 'block')
copyForm.setAttribute('id', `form-${totalForms}`)
const regex = new RegExp('__prefix__', 'g')
copyForm.innerHTML = copyForm.innerHTML.replace(regex)
totalNewForm.setAttribute('value', totalForms + 1)
formCopyTarget.append(copyForm)
}
Вам не хватает атрибута action
в вашем HTML.
<form method="POST" id="form" action="{% url 'your-url-name-here' %}">