0

I got a django-form with an image field. I load this in a jquery dialog-modal. I have a js-function which submits this form, populates some fields, shows the validation errors. But from what i understand it's not possible to post/submit the imagefields with Ajax?

Is there a simple solution / work around to post this without a total reconstruction of the other functions?

Thank you!

user3199840
  • 525
  • 2
  • 13
  • 35
  • Why not? What have you tried? – Brandon Taylor Jan 31 '14 at 16:17
  • Not sure why I've read some other post here on stackoverflow about imageupload and ajax and that it wasn't possible. I have to override the regluar form submit with a js-function because i have a dialog and i want the validations errors to be sent back to the same dialog. I've tried post it like before but changing the form to enctype="multipart/form-data" and tried to change ajax-data to formData. – user3199840 Jan 31 '14 at 17:16
  • Submitting a form is submitting a form, whether you're initiating the submit with JavaScript or not. Something I wrote to help in situations like this, where you need to deal with form errors in an ajaxy style was a set of Ajax Form Base Classes that could return errors as JSON: https://djangosnippets.org/snippets/2393/ FWIW, that might get you pointed in the right direction. – Brandon Taylor Jan 31 '14 at 19:31
  • Ah thank you. I'm still kind of new to all of this. But i already got the form submit + the validation errors in place (returned as json). That part is working. I'm just wondering how to add the imagefield to all of this. But are you saying is shouldn't be a problem POSTing it with ajax together with the rest of the form? – user3199840 Jan 31 '14 at 20:26
  • No it won't be a problem, the input for the file/image field will be sent with the post whether it's sent via ajax or not. – Brandon Taylor Jan 31 '14 at 20:32
  • OK. Hmm. The problem must be somewhere else then. Back to the lab... Sometimes i get the "this field is required" error from the imagefield so just thought is wasn't posting that at all. – user3199840 Jan 31 '14 at 21:38
  • That's a validation error on the form class. If the value of the selected file isn't being maintained for some reason, there's another problem going on. If you're submitting the form via ajax, that fields shouldn't end up blank after a submit. – Brandon Taylor Feb 01 '14 at 00:35
  • Ok. Could it be that im not handling the image field it correctly in the views? – user3199840 Feb 01 '14 at 01:24
  • Not sure. You'd need to add the relevant portions of code to your question. – Brandon Taylor Feb 01 '14 at 01:55
  • OK im posting my view. Please have look! (may be some identation errors due to the posting) – user3199840 Feb 01 '14 at 02:16
  • Brandon, Would you please mind just giving me a hint on how to do this ajax post submission with the imagefield included. I've checked my form, model and view and everthing works with a normal submitbutton but when i try with js it's just telling me the field is empty. Yes im desperate. – user3199840 Feb 03 '14 at 18:39
  • Hi. Sorry I wasn't able to get back to you over the weekend. Could you please send me a Gist with your view and form code? – Brandon Taylor Feb 03 '14 at 20:05

0 Answers0