views:

172

answers:

2

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..

A: 

If I understand rightly, you're sniffing the return code in the JavaScript, and then redirecting depending on the results.

You can do a redirect from django, so I would do that instead of worrying about return codes. When you've got both a "flag" and a "ret_status", that is a hint you should re-think your design. :)

Also, shadowing the built-in dict object in the python code should be avoided.

Ryan Ginstrom
A: 

If you do the response like you said:

return render_to_response('mypjt/display.html',context_instance=RequestContext(request,{'dict': dict}))

The javascript will receive your response, not the navigator. I think you can do somethink like this:

<script>
   $(document).ready(function()
   {
      $('#yourForm').submit();
   });
</script>

<form id="yourForm" action="/mypjt/my_timer" method="post">
...
your fields with data, even they are hidden
...
</form>

So, in django you can do the response like you said:

 def my_timer(request):
     dict={}
     try:
         a=  timer.objects.get(pk=1)

         dict({'flag':1})
         return render_to_response('mypjt/display.html',context_instance=RequestContext(request,{'dict': dict}))
     except:
         dict({'flag':0})
         return render_to_response('mypjt/error_not_found.html',context_instance=RequestContext(request,{'dict': dict}))

Or, you can do like you were doing but if the query "timer.objects.get(pk=1)" fails, for example, you send back a boolean flag response. So, when it is ok you redirect to the page you prefer.

I hope it could be useful to you!

Jayme