Pokaż wyjście widoków w HTML za pomocą Ajax, Django
#views.py
def ajaxUpload(request):
predictedLabel = predictimage(request)
print("success")
context = {"predictedLabel": predictedLabel}
return HttpResponse(json.dumps(context)) #dump the context into json
#javaScript
$("#theForm").submit(function uploadFile(e){
e.preventDefault();
var data=new FormData();
data.append("file",$("#inpFile")[0].files[0]);
data.append("csrfmiddlewaretoken", "{{csrf_token}}");
$.ajax({
method:"POST",
url:"/ajaxupload/",
processData:false,
contentType:false,
mimeType:"multipart/form-data",
data:data,
success:function(response){
var obj=JSON.parse(response)["predictedLabel"]; #parse the json with name of the context you want
$("#predictedOutput").text(obj)
}
})
})
Incursio