By pressing the submit button of the form. go to the error function of the script.js file, when looking at console " POST / HTTP / 1.1" 200 28518 , the database is postgres. is not saving the data, I have reviewed several pages online but I can not find the solution, I just want to send the data without reloading the page any ideas?
script.js
var clContactForm = function() {
/* local validation */
$('#contactForm').validate({
/* submit via ajax */
submitHandler: function(form) {
var sLoader = $('.submit-loader');
$.ajax({
type: "POST",
url: $(this).attr(''),
data: $(form).serialize(),
beforeSend: function() {
sLoader.slideDown("slow");
},
success: function(msg) {
// Message was sent
if (msg == 'OK') {
sLoader.slideUp("slow");
$('.message-warning').fadeOut();
$('#contactForm').fadeOut();
$('.message-success').fadeIn();
}
// There was an error
else {
sLoader.slideUp("slow");
$('.message-warning').html(msg);
$('.message-warning').slideDown("slow");
}
},
error: function() {
sLoader.slideUp("slow");
$('.message-warning').html("Something went wrong. Please try again.");
$('.message-warning').slideDown("slow");
}
});
}
});
};
form.html
<form name="contactForm" id="contactForm" method="post" >
{% csrf_token %}
<fieldset>
{{form.nombre}}
{{form.email}}
{{form.telefono}}
{{form.mensaje}}
<div class="form-field">
<button class="full-width btn--primary">Submit</button>
<div class="submit-loader">
<div class="text-loader">Sending...</div>
<div class="s-loader">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
</div>
</div>
</div>
views.py
def contacto(request):
contactos = Contacto.objects.all()
if request.method == 'POST':
formulario = ContactoForm(data=request.POST)
if formulario.is_valid():
formulario.save()
else:
formulario = ContactoForm()
return render(request, 'formulario.html', {'contactos': contactos, 'form': formulario})