managment formset for new device

This commit is contained in:
Cayo Puigdefabregas 2024-07-22 13:48:10 +02:00
parent 0cffa5a951
commit 45ebb6f169
2 changed files with 52 additions and 35 deletions

View File

@ -8,6 +8,17 @@
</div>
</div>
<script>
function addForm(button) {
var formCount = parseInt(document.getElementById('id_form-TOTAL_FORMS').value);
var formCopy = $(document.querySelector('#id_form-0-name')).parent().parent().parent()[0].cloneNode(true);
// formCopy.style.display = 'block';
formCopy.innerHTML = formCopy.innerHTML.replace(/__prefix__/g, formCount);
document.getElementById('formset-container').appendChild(formCopy);
document.getElementById('id_form-TOTAL_FORMS').value = formCount + 1;
}
</script>
{% load django_bootstrap5 %}
<form role="form" method="post">
{% csrf_token %}
@ -22,34 +33,33 @@
</div>
</div>
{% endif %}
{# bootstrap_form form #}
<div class="form-actions-no-box">
{{ form.management_form }}
<div class="container" id="formset-container">
<div class="row mb-2">
<div class="col"></div>
<div class="col-2 text-center">
<a href="javascript:void()" onclick="addForm(this);" type="button" class="btn btn-green-admin">
<i class="bi bi-plus"></i>
{% trans 'Add' %}
</a>
</div>
</div>
{% for f in form %}
<div class="row mb-2">
<div class="col">
{% bootstrap_field f.name %}
</div>
<div class="col">
{% bootstrap_field f.value %}
</div>
</div>
{% endfor %}
</div>
<div class="container">
<a class="btn btn-grey" href="{% url 'dashboard:unassigned_devices' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
</form>
<script>
function addForm(button) {
var formCount = parseInt(document.getElementById('id_form-TOTAL_FORMS').value);
var formCopy = document.querySelector('.dynamic-form').cloneNode(true);
formCopy.style.display = 'block';
formCopy.innerHTML = formCopy.innerHTML.replace(/__prefix__/g, formCount);
document.getElementById('formset-container').appendChild(formCopy);
document.getElementById('id_form-TOTAL_FORMS').value = formCount + 1;
}
</script>
<form method="post">
{% csrf_token %}
<div id="formset-container">
{% for f in form %}
<div class="dynamic-form" style="display: {% if forloop.first %}block{% else %}none{% endif %};">
{{ f.as_p }}
</div>
{% endfor %}
</div>
<button type="button" onclick="addForm(this);">Add</button>
<button type="submit">Submit</button>
</form>
{% endblock %}

View File

@ -21,14 +21,21 @@ class NewDeviceView(DashboardView, FormView):
template_name = "new_device.html"
title = _("New Device")
breadcrumb = "Device / New Device"
success_url = reverse_lazy('dashboard:unassigned_devices')
success_url = reverse_lazy('device:add')
# success_url = reverse_lazy('dashboard:unassigned_devices')
form_class = DeviceFormSet
def form_valid(self, form):
form.instance.owner = self.request.user
# import pdb; pdb.set_trace()
# form.instance.owner = self.request.user
response = super().form_valid(form)
return response
def form_invalid(self, form):
import pdb; pdb.set_trace()
response = super().form_invalid(form)
return response
# class AddToLotView(DashboardView, FormView):
# template_name = "list_lots.html"