Im having some problems submitting a form (Django-form) with a imagefield with Jquery/ajax POST. Im getting back "Field required" validation errors on the imagefield..
I been trying different solutions with appending formData but with no results so far. Am i on the right track? Please point me in the right direction. Thank you!
Update: I can set the imagefield to required=false and not get the validation error but i want the field to be required and it seems like the form still doesn't submit the image..
The basic function looks like this:
$(function() {
$('#imageupload').on('click', function() {
$('#dialog-modal').load('upload/ #myform');
$('#dialog-modal').dialog({
height: 550,
width: 280,
modal: true,
buttons: {
Send: function() {
var dialog = $(this),
form = $('#myform'),
data = form.serialize();
$('.off').remove();
$.ajax({
url: 'upload/',
data: data,
type: 'post',
success: function(response) {
res = $.parseJSON(response);
if (res['status'] == 'OK') {
alert('Thank you!');
dialog.dialog('close');
}
else if (res['status'] == 'bad') {
alert('Please try again!');
delete res['status']
var errors = res;
$.each(errors, function(key, value) {
var err = $('<span></span>', {
'class': 'off',
'text': value
}),
br = $('<br></br>', {
'class': 'off',
}),
input = $('#id_'+key).parent();
br.appendTo(input);
err.appendTo(input);
err.css('color', 'red').css('font-size', '10px');
});
}
}
});
}
}
});
});
})
Form looks like this, it's in a child html wich is loaded into a jquery dialog/modal box:
<form method="post" id='myform' enctype="multipart/form-data">
{% csrf_token %}
<h1> Upload </h1>
<p><label for="name">Name:</label></p>
<div class="fieldWrapper">
{{ form.name }}
</div>
<br>
<p><label for="type">Type:</label></p>
<div class="fieldWrapper">
{{ form.type }}
</div>
<br>
<p><label for="description">Description:</label></p>
<div class="fieldWrapper">
{{ form.description }}
</div>
<br>
<p><label for="picture">Picture:</label></p>
<div class="fieldWrapper">
{{ form.picture }}
</div>
</form>