jquery - Django/Python: Not being able to send file to the view -


i have form:

<form  id="formi"  method="post" enctype="multipart/form-data">  {% csrf_token %}    <div>       <input id="some_file_input" type="file"  name="some_file"">    </div>    <button type="submit" id="upload-file-btn">submit</button> </form> 

this view, receives file form:

def d_s_w(request):      if request.is_ajax() , request.method == "post":          if len(request.files) != 0:              data = request.files['some_file']              ...              context = {'dates': dates, 'users': users}              data = json.dumps(context)              return httpresponse(data, content_type="application/json")         else:             raise http404("no file uploaded")     else:         raise http404("no post data given.") 

i not want page reload again after submitting file want use ajax , jquery this. call view through url , execute code. url:

url(r'^$', views.index, name='index'), ------->this page form, not want page reload  url(r'^work/$', views.d_s_w, name='d_s_w'),--->page points view takes file , work file. 

to using code:

<script>     $("#formi").submit(function(event){        event.preventdefault();         $.ajax({              type:"post",              url:"{% url 'd_s_w' %}",              data: {                     file: $('form').serialize(),                     },              success: function(data){                     console.log("success")                 },               error : function(xhr) {                     console.log(xhr.status + ": " + xhr.responsetext); // provide bit more info error console                 }         });    }); 

so, running see view called correctly post method, there no file getting view, request empty:

enter image description here

obviously doing or many things wrong can not see where.

thanks in advance help

ok, how managed send file view:

$("#formi").submit(function(event){    event.preventdefault();     var data = new formdata($('form').get(0));          $.ajax({              type:"post",              url:"{% url 'd_s_w' %}",              data: data,              processdata: false,              contenttype: false,              csrfmiddlewaretoken: '{{ csrf_token }}',               success: function(data){                     console.log("success")                 },               error : function(xhr) {                     console.log(xhr.status + ": " + xhr.responsetext); // provide bit more info error console                 }         });    }); 

now getting there , code running.

i not being able see data being returned view, work on it. e comments


Comments