Django and json request
Posted
by Hulk
on Stack Overflow
See other posts from Stack Overflow
or by Hulk
Published on 2010-05-12T20:40:56Z
Indexed on
2010/05/12
20:44 UTC
Read the original article
Hit count: 298
In a template i have the following code
<script>
var url="/mypjt/my_timer"
$.post(url, paramarr,
function callbackHandler(dict)
{
alert('got response back');
if (dict.flag == 2)
{
alert('1');
$.jGrowl("Data could not be saved");
}
else if(dict.ret_status == 1)
{
alert('2');
$.jGrowl("Data saved successfully");
window.location = "/mypjt/display/" + dict.rid;
}
},
"json"
);
</script>
In views i have the following code,
def my_timer(request):
dict={}
try:
a= timer.objects.get(pk=1)
dict({'flag':1})
return HttpResponse(simplejson.dumps(dict), mimetype='application/javascript')
except:
dict({'flag':1})
return HttpResponse(simplejson.dumps(dict), mimetype='application/javascript')
My question is since we are making a json request and in the try block ,after setting the flag ,cant we return a page directly as
return render_to_response('mypjt/display.html',context_instance=RequestContext(request,{'dict': dict}))
instead of sending the response, because on success again in the html page we redirect the code
Also if there is a exception then only can we return the json request.
My only concern is that the interaction between client and server should be minimal.
Thanks..
© Stack Overflow or respective owner