简体   繁体   中英

request.is_ajax not executing render_to_response django

I am having a small django view that responds with an dict, on an ajax call . Here is the code below :

Ajax Call :

var getdata = function(e){
                var id = this.id;
                console.log(id);
                $.ajax({
                    url:'/editcontact/',
                    method :'POST',
                    data :{
                        'name' : id,
                    },
                    success:function(){
                        console.log("success");
                    },
                    error:function(status){
                        console.log (status);
                    }
                });
            };
            $("button").on('click',getdata);

views.py

if request.is_ajax:
print "comes here"
value1 = request.GET['name']
print value1
data = dbs.objects.filter(cname=value1)
print data
return render_to_response("editcontact.html",{"data":data}, context_instance=RequestContext(request))

the code is executing till print data, but the render_to_response is not working.

I will suggest you to return response in json format. that will solve your problem. you can take help from below given code and can change your views.py

import json
if request.is_ajax:
    print "comes here"
    value1 = request.GET['name']
    print value1
    data = dbs.objects.filter(cname=value1)
    json = simplejson.dumps(data)
    return HttpResponse(json, mimetype='application/json')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM