Python Django,带有额外数据的ajax上传文件

Python Django,带有额外数据的ajax上传文件,python,ajax,django,Python,Ajax,Django,我有一个小问题,我想上传一个包含一些额外数据的文件 所以我现在要做的是: JS文件: Views.py 打印数据: 文件-> 数据->无 因此,我需要将文件发送到外部服务器,这需要一些额外的信息,但我不知道如何组合这些数据。我猜您需要设置请求的内容类型。在AJAX调用中,传递的是contentType:false。也许可以尝试将其更改为contentType:“多部分/表单数据”?我猜您需要设置请求的内容类型。在AJAX调用中,传递的是contentType:false。也许可以尝试将其更改为

我有一个小问题,我想上传一个包含一些额外数据的文件

所以我现在要做的是:

JS文件: Views.py 打印数据:
文件->
数据->无

因此,我需要将文件发送到外部服务器,这需要一些额外的信息,但我不知道如何组合这些数据。

我猜您需要设置请求的内容类型。在AJAX调用中,传递的是
contentType:false
。也许可以尝试将其更改为
contentType:“多部分/表单数据”

我猜您需要设置请求的内容类型。在AJAX调用中,传递的是
contentType:false
。也许可以尝试将其更改为
contentType:“多部分/表单数据”

您没有因为csrf令牌而收到错误吗?您没有因为csrf令牌而收到错误吗?
$(document).on('submit', '.upload-file', function(e){
    e.preventDefault();

    var form_data = new FormData($(this)); //I dont know why, but it used to return None
        //so I append file and some extra data
        form_data.append( 'file', ($(this).find('[type=file]')[0].files[0]) );
        form_data.append( 'expire', ($(this).find('.document-id').val()) );
        form_data.append( 'document', ($(this).find('[type=datetime-local]').val()) );

    $.ajax({
        type : 'POST',
        url : 'some_url',
        data : form_data,
        cache: false,
        processData: false,
        contentType: false,
    }).done(function(data) {
        console.log(data);      
    });

});
def upload_file(request):
    if request.method =='POST':
        data = request.POST.get('data')
        file = request.FILES

        print('File ->', file) #this one prints File
        print('Data ->', data) #this one prints None

        return HttpResponse()
 File -> <MultiValueDict: {'file': [<InMemoryUploadedFile: MyFile.docx (application/vnd.openxmlformats-officedocument.wordprocessingml.document)>]}>
 Data -> None