I am facing a problem while attempting to send an image through an AJAX request to Django. Here is my HTML:
<form>
<input type="file" id="files" name="image">
</form>
Here is my JS :
var control = document.getElementById("files");
var p = {
title: $('input[name=title]').val(),
subtitle: $('input[name=subtitle]').val(),
content: $('textarea#article-content').val(),
image: files[0],
};
$.ajax({
url: '/prive/nouveau-projet',
type: "POST",
data: JSON.stringify(p),
crossDomain: true,
success: function() {
window.location.href = '/prive/projets/';
},
error: function() {
console.log("error");
}
});
And here is my server-side code:
if request.method == "POST":
data = request.POST.keys()[0]
dataJson = json.loads(data)
p = Projet(title=dataJson['title'], subtitle=dataJson['subtitle'], content=dataJson['content'], image=dataJson['image'])
p.save()
return HttpResponse()
That is what I tried but I get errors about dataJson['image']. Could you help me please ?