0

Background : I have a simple model form with a image field.

class XYZForm(ModelForm):
    class Meta:
        model = XYZ
        fields = ( 'file',)

The model is

class XYZ(models.Model):
    url = models.URLField(null = True)
    file = models.ImageField(upload_to=get_photo_storage_path, null = True)

Problem: I am submitting this form via ajax. But this form is not validating, as the file is not being stored.Is there a particular way of submitting forms with files via ajax ?

P.S. :- request.FILES is empty in the view, where the form is being submitted

Akash Deshpande
  • 2,583
  • 10
  • 41
  • 82

3 Answers3

2

Files can't be send using ajax, you can mimic an ajax upload by using a iframe for uploading.

Nice example: http://embrangler.com/2010/08/ajax-uploads-images-in-django/

Willian
  • 2,385
  • 15
  • 17
1

you can try to use django-swfupload:

http://swfupload.googlecode.com

yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42
  • Do we need to add a plugin even for a single image upload ? – Akash Deshpande Mar 12 '12 at 06:43
  • I just thought looking/using some solution would help you recognize your problem. Because you didn't show almost any code to see where can problem be. Ie - don't you miss enctype="multipart/form-data" parametr in form element? How does your ajax call looks like? etc. – yedpodtrzitko Mar 12 '12 at 10:35
1

I used this one https://github.com/GoodCloud/django-ajax-uploader But I used without It is pretty easy to use,

Eugene Nagorny
  • 1,626
  • 3
  • 18
  • 32