开发者

Django and JSON request

开发者 https://www.devze.com 2022-12-30 06:25 出处:网络
In a template I have the following code. <script> var url=\"/mypjt/my_timer\" $.post(url, paramarr,

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')

Since we are making a JSON request and in the try block, after setting开发者_运维知识库 the flag, can't 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.


If you do the response like you said,

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

the JavaScript code 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消