Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Show output of views in html using 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)
                 }
             })
        })
Comment

PREVIOUS NEXT
Code Example
Python :: seaborn histogram normalize 
Python :: online python text editor 
Python :: open weather get local time python 
Python :: right click vs left click pygame 
Python :: etails of the Response object by using help() method 
Python :: getting month number in python 
Python :: wait until you press escape 
Python :: how to vreate a list in python 
Python :: admin email errors 
Python :: Python Pipelining Generators 
Python :: Python ValueError in strptime() 
Python :: prevent not admin from visiting a url tornado python 
Python :: How to provide type hinting in UserDict 
Python :: Python send sms curl 
Python :: python check if array alternating 
Python :: heksadesimal ke ascii 
Python :: rickroll on input IN PYTHON 
Python :: big python code 
Python :: Plotting a dendrogram 
Python :: how to add strings with entry in tkinter 
Python :: rpi pip3 installieren 
Python :: how to choose appropriate graph for your dataset visualization 
Python :: what will be the output of the following python code? x = 123 for i in x: print(i) 
Python :: ex: git push new local repo 
Python :: How to Use the abs() Function with an Integer Argument 
Python :: 5.2.5: Counting 10 to 100 by ... 
Python :: tkinter centre a button on 1920x1080 
Python :: if user_answer==answer: ecpeted index erroe pythin fx 
Python :: gui apps 
Python :: how to correct spelling in pandas datafeame 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =