Re: Problems doing ajax post with ImageField

2013-02-10 Thread Jani Tiainen

Hi,

There is no way to save files through XHR, (not at least without HTML5 
features).


One quite common approach is to use temporary, hidden iframe where form 
is duplicated along the filefield and then submit that form as normal 
page submit and then to read response one way or another.


I don't know exact procedure how that works in jquery but maybe this 
helps: http://bit.ly/11AA3dW


11.2.2013 0:28, bobhaugen kirjoitti:

Here's the relevant parts of the model:

class Resource(models.Model):
 photo = ThumbnailerImageField(_("photo"),
 upload_to='photos', blank=True, null=True)

This works fine in admin and with regular form posts.

Trying to use jquery.post in one situation to post a photo without
suffering a page reload.

With the regular post, the photo field returns like this (in
form.cleaned_data):
'photo': 

And request.FILES is like this:
]}>

With the jquery post, photo is None and the MultiValueDict is empty.

In both cases the form is set up with enctype="multipart/form-data",
and the form is instantiated in the view with
form = ResourceForm(request.POST, request.FILES)

What am I doing wrong?

Here's the javascript:

$("#detailForm").submit(function(event) {
event.preventDefault();

var $form = $( this ),
url = $form.attr( 'action' );

var formData = $form.serialize();
saveResource(url, formData);

});

function saveResource(url, formData)
{
notifySaving();
var jqxhr = $.post( url, formData,
function( data ) {
$('#detailModal').modal('hide');
notifySaved();
resourceSaved = true;
})
.fail(function()
{
$('#detailModal').modal('hide');
notifyProblem();
resourceSaved = false;
startRetrying();
}
);
}

--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.





--
Jani Tiainen

- Well planned is half done and a half done has been sufficient before...

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Problems doing ajax post with ImageField

2013-02-10 Thread bobhaugen
Here's the relevant parts of the model:

class Resource(models.Model):
photo = ThumbnailerImageField(_("photo"),
upload_to='photos', blank=True, null=True)

This works fine in admin and with regular form posts.

Trying to use jquery.post in one situation to post a photo without 
suffering a page reload.

With the regular post, the photo field returns like this (in 
form.cleaned_data):
'photo': 

And request.FILES is like this:
]}>

With the jquery post, photo is None and the MultiValueDict is empty.  

In both cases the form is set up with enctype="multipart/form-data", 
and the form is instantiated in the view with 
form = ResourceForm(request.POST, request.FILES)

What am I doing wrong?

Here's the javascript:

$("#detailForm").submit(function(event) {
event.preventDefault();

var $form = $( this ),
url = $form.attr( 'action' );

var formData = $form.serialize();
saveResource(url, formData); 

});

function saveResource(url, formData)
{
notifySaving();
var jqxhr = $.post( url, formData,
function( data ) {
$('#detailModal').modal('hide');
notifySaved();
resourceSaved = true;
})
.fail(function() 
{ 
$('#detailModal').modal('hide');
notifyProblem(); 
resourceSaved = false;
startRetrying();
}
);
}

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.