I am following the instructions from django-bootstrap-modal-forms and what I am finding is that my form is posting or submitting twice when I submit the form. First, the object was simply being created twice, I could see that from the admin. Now it seems the form is creating the object, but also entering its validation state, which I obviously don't want if the form was successful, which it is.
Has anyone experienced this? I've done nothing more than what was outlined in the docs that I linked to and I cannot identify any reason why this should be submitting twice.
Here is my code:
home.html
<a href="#" class="create-shipment dropdown-itemcreatenew" onclick="closeNav()">Shipment</a>
<div class="modal fade" tabindex="-1" role="dialog" id="modal">
<div class="modal-dialog" role="document">
<div class="modal-content">
</div>
</div>
</div>
<script>
$(document).ready(function() {
$(".create-shipment").modalForm({
formURL: "{% url 'CreateShipmentView' %}"
});
});
</script>
views.py
class CreateShipmentView(BSModalCreateView):
template_name = 'create_shipment.html'
form_class = CreateShipmentForm
success_message = 'Success: Shipment Has Been Created!'
success_url = reverse_lazy('HomeView')
create_shipment.html (template for modal form)
{% load crispy_forms_tags %}
<form method="post" action="">
{% csrf_token %}
<div class="modal-header">
<h5 class="modal-title">Create New Shipment</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{form|crispy}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="submit-btn btn btn-primary">Create</button>
</div>
</form>
forms.py
class CreateShipmentForm(BSModalForm):
class Meta:
model = Shipment
fields = ['Reference_Number', 'Ultimate_Consignee']
urls.py
url(r'^create_shipment/', views.CreateShipmentView.as_view(), name='CreateShipmentView'),
Event Listeners on Submit Button
// Add click listener to the submitBtn
var ajaxSubmit = function (modalID, modalContent, modalForm, formURL, errorClass, submitBtn) {
$(submitBtn).on("click", function () {
// Check if form.is_valid() via ajax request when submitBtn is clicked
isFormValid(modalID, modalContent, modalForm, formURL, errorClass, submitBtn, submitForm);
});
};
// Check if form.is_valid() & either show errors or submit it
var isFormValid = function (modalID, modalContent, modalForm, formURL, errorClass, submitBtn, callback) {
$.ajax({
type: $(modalForm).attr("method"),
url: $(modalForm).attr("action"),
// Serialize form data
data: $(modalForm).serialize(),
success: function (response) {
if ($(response).find(errorClass).length > 0) {
// Form is not valid, update it with errors
$(modalID).find(modalContent).html(response);
$(modalForm).attr("action", formURL);
// Reinstantiate click listener on submitBtn
ajaxSubmit(modalID, modalContent, modalForm, formURL, errorClass, submitBtn);
} else {
// Form is valid, submit it
callback(modalForm);
}
}
});
};