I want to update an object via UpdateAPIView using Django Rest framework
$('#update-form').on('submit', function(event){
event.preventDefault();
console.log( $( this ).serialize() );
data = new FormData($('#update-form')[0]);
$.ajax ({
type: "PATCH",
url: $( this ).attr( 'action' ),
data: data,
processData: false,
contentType: false,
success: function(json) {
console.log(json);
console.log('success');
},
error: function(xhr, errmsg, err) {
console.log(xhr.status + ": " + xhr.responseText)
}
});
})
Upon submitting, I get the error response:
403: {"detail":"CSRF Failed: CSRF token missing or incorrect."}
I have {% csrf_token %} in the templates, and verify its there, either by looking at the source of the html, and printing console.log of the serialized data, where csrf token is there.
Checking the template context, the csrf token is there. Settings have the csrf middleware added.
the form html is long, but in short, the csrf is there, but the endpoint (DRF) complains of missing csrf
And by the way, creating an object using the CreateAPIView, using same form, but just the form id changed, succeeds, yet the update view doesn't work. The CreateAPIView doesn't complain of any csrf things.