我将使用jquery ajax编辑ImageField,搜索后发现我应该使用jQuery表单插件,所以我认为在本文中使用引入的方法是好:http://www.laurentluce.com/posts/upload-to-django-with-progress-bar-using-ajax-and-jquery/ 但我想获得POST图像的数据,并给它一个模型的形式来处理它,因为它采取的每一件事情照顾,即使反复文件的名称,有些事情是这样的:使用jquery ajax获取提交文件的数据并使用ModelForm处理它
var options = {
dataType: 'xml',
url: '{% url DrHub.views.editNews param1,param2 %}',
beforeSubmit: showRequest,
success: showResponse
}
$('#form_upload').ajaxSubmit(options);
并在服务器端:
if ('id_HeadImage' in request.FILES) and (request.FILES['id_HeadImage']):
gForm=GalleryForm(request.FILES['id_HeadImage'],instance=newsInstance.gallery_ptr)
if gForm.is_valid():
gForm.save()
因为U可以看到我要编辑名为Gallery 的模型的ImageField,但这种方式我只是获取文件名。
我该怎么做?
编辑
这是画廊型号:
class Gallery(models.Model):
HeadImage = models.ImageField(upload_to="gallery",blank=True,null=True)
编辑
Request.FILES是空的,AJAX只是后的文件名,而不是数据!