django file upload from json

2019-03-05 03:57发布

问题:

Hi i have given the complete code for my file upload .But in my views i am just trying to return a response and get the alert on the UI.But none of the return statements are working.How to rectify this..

EDIT : i see the logging statements on the server side

  <h1>Add Content</h1>
  <form  method="post" enctype="multipart/form-data" id="contentform" action="/content/savecontent/" >{% csrf_token %}
  <b>
  <table>

   <tr><td><font>*</font>Content Name</td><td> <input type="text" id="cname" name="cname" maxlength="30"/></td></tr>
   <tr><td><font>*</font>Description</td><td><input type="text" id="cdescription" name="cdescription" maxlength="30"/> </td></tr>
   <tr><td><font>*</font>Type</td><td> 
   <select id="type" name="type">
   <option value="1" selected>Local</option>      
   <option value="2">Internet</option>      
   </select>
   </td></tr>
   <tr><td><font>*</font>Access</td><td>
   <select id="access" name="access">
   <option value="1" selected>Public</option>     
   <option value="2">Private</option>    
   </select>

   </td></tr>
   <tr><td><font>*</font>Content</td><td id="carea"> </td></tr>
   <tr><td></td><td><input type="button" value="Upload" id="addbtn" onclick="ajax_upload('#contentform');"/></td></tr>
  </table>
  </b>
  </form>

 <script>
 $(document).ready(function() {
 $('font').css({'color':'red'})
 $("#carea").html('').append('{{form.file_upload}}');
 });

  function ajax_upload(formid)
  {
     var form = $(formid);
     form.ajaxSubmit({
        dataType:  'json',
        success:   function (data) {
        alert("hereeeeeee");
        }
     } )   ;
  }
  </script>

views.py

def savecontent(request):
 response_dict={'status':1}
 logging.debug(request)
 logging.debug("========================================")
 return render_to_response('content/confirmation.html', context_instance=RequestContext(request, {'response_dict':response_dict}))
 #return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
 #return HttpResponse(simplejson.dumps(response_dict), mimetype='text/html')
 #return HttpResponse(simplejson.dumps(response_dict), mimetype='application/json')

回答1:

Check that you have no errors in web browser console. Perhaps the problem in ajaxSubmit function. Try change ajaxSubmit to:

$.ajax({
    url: form.attr('action'),
    data: form.serialize(),
    type: 'POST',
    success: function(data){
        alert(data);
    }
});

and check result.