我有一个带有新闻源功能的 Django webapp。新闻源具有与之相关的标题、正文和图像。用户可以编辑和更新他们的新闻源,包括添加新图像。
目前我正在获取每个提要项目的所有数据,如下所示:
var newsfeeds = [];
$('.feed').each( function (index, element) {
var feed_dict = {'title': $(this).children('#title').val(),
'id': $(this).children('#feed_' + index + '_id').val(),
'should_delete': $(this).children('#should_delete').val(),
'subtitle': $(this).children('#subtitle').val(),
'body': $(this).children('#body').val()};
newsfeeds.push(feed_dict);
});
然后使用$.ajax()
. 这很好用。
现在我正在处理图像的东西。每个提要都有一个<input type="/>
与唯一 ID 相关联的关联,因此我应该能够获取每个文件。那么问题是如何发送它们,以便我可以获取 Djangorequest.POST
和request.FILES
. 我试过FormData
在 JS 中使用这样的对象:
var data = new FormData();
$('.image_input').each(function(index, element) {
data.append('file_' + index, element.files);
});
但这给了我一个NS_ERROR_CANNOT_CONVERT_DATA
Firebug。
在这一点上,我很迷茫。任何指针都会非常有帮助。