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

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

Comments

1

you can try to use django-swfupload:

http://swfupload.googlecode.com

2 Comments

Do we need to add a plugin even for a single image upload ?
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.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.