Wagh
Wagh

Reputation: 4306

send image in http response to ajax response getting error imagefilefield is not JSON serializable

I want to send the image to ajax responce from the view.

view.py

def Display_post(request):
    if request.method=="GET":
        post = Post.objects.all()
        print post
        data = []
        for pos in post:
            postdata = pos.post
            userdata = pos.user.username
            postimage = pos.image
            print type(postimage)


            record = {"postdata":postdata, "userdata":userdata, "postimage":postimage}
            data.append(record)
        response = json.dumps(data,cls=DjangoJSONEncoder)
        return HttpResponse(response, mimetype="application/json")

I am going into ajax error function. How to get image in ajax success?

ajax success is

success:function(response){

                                                    alert(response[0].userdata);
                                                    len = response.length;
                                                    alert(len);
                                                    for(i=0; i<=len;i++)
                                                    {
                                                        post = response[i].postdata;
                                                        user = response[i].userdata;
                                                        image = response[i].postimage;
                                                        $(".posts").append('<div class="col-md-4"><img src="'+image+'" width="100" height="100" /></div>div class="col-md-2">'+user+'</div><div class="col-md-6">'+post+'</div>');
                                                    }
                                },  

please help me for this,,,,,,,,,,,

Upvotes: 0

Views: 310

Answers (1)

Rodrigo Techera
Rodrigo Techera

Reputation: 251

The image object is not json serializable that's it the problem, but if insted of the image object you just send via AJAX the image url, I think the problem will be solved. Try, in your view.py, changing this: "postimage":postimage to this:"postimage":postimage.url

Upvotes: 2

Related Questions