从json上传django文件 [英] django file upload from json
本文介绍了从json上传django文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经给出了文件上传的完整代码.但在我看来,我只是想返回一个响应并在 UI 上获得警报.但是没有一个返回语句起作用.如何纠正这个..
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
:我看到服务器端的日志记录
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')
推荐答案
检查 Web 浏览器控制台中是否没有错误.也许是ajaxSubmit 函数的问题.尝试将 ajaxSubmit 更改为:
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);
}
});
并检查结果.
这篇关于从json上传django文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文