2
I believe you need to do something like this:
$('#form').on('submit', function(event){
event.preventDefault();
$form = $('#form');
$.ajax({
type: $form.attr('method'),
dataType: "JSON",
url: $form.attr('action'),
data: {
form: $form.serialize()
}
}).done( function( response ) {
console.log('ok');
$( '#old' ).remove();
$( '#contact' ).append(response.content);
})
});
You need to prevent the form submission.
Another way to do this is maybe by using jQuery Form Plugin
Next thing you should do is to add CSRFToken to your Ajax header, more on this here
0
Make sure you are sending the csrfmiddlewaretoken with your ajax request if itβs a post. Otherwise you will get a 403 and possibly a redirect.
A quick way to test for this is to add the @csrf_exempt
decorator to your view.
- [Answer]-Adding FieldFile objects without form in django
- [Answer]-Images into a template in Django 1.8
- [Answer]-Send email django from any host
Source:stackexchange.com