S
S
Siberian_12018-09-29 12:39:13
Django
Siberian_1, 2018-09-29 12:39:13

How to send file(s) using AJAX with Jquery and get them in Django view?

//так я получаю текст инпут
var text = $('#form-new-task-z-r-text').val(); 

//тут мне надо получить инпут с файлами
//<input type="file" name="file" multiple="" required="" id="file_record">
//var my_file = ...

$.ajax({
  type: 'POST',
   url: 'add_new_rec/'+prot_id+'/'+id_q+'/',
  data: {csrfmiddlewaretoken: getCookie('csrftoken'), 'r_text':text, 'file':my_file},     //'file' - это мои файлы
  success: function(data) {
        }	
});


def add_new_rec(request,id_prot,id_q):
    text = request.POST.get("r_text")
    #Как принять мой(и) file ?


So that I then add them to the model object in a cycle.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Web Dentist, 2018-09-29
@kgb_zor

jQuery.ajax({
        type: 'POST',
        url: 'add_new_rec/'+prot_id+'/'+id_q+'/',
        async: true,
        cache: false,
        processData: false,
        contentType: false,
        enctype: 'multipart/form-data',
        data: {csrfmiddlewaretoken: getCookie('csrftoken'), 'r_text':text, 'file':my_file},

        success: function (data) {
}
});

views.py
model.field = request.FILES['my_file']
model.save()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question