Friday, December 26, 2014

How to make Ajax calls from jquery client side to django view

Problem - We need to make Ajax call from client side browser to the server posting some data. Instead of posting the whole form to the server we need to use ajax to post only the required data back to the server. The problem arises in how to provide URL parameter for the post call which includes the server root and the app route as well.

Solution - 

  • Let the form include the csrf token as provided by django framework by placing {% csrf_token %}
  • The javascript function that will make the required call at :

          var host = document.location.host; // host + port
          var protocol = document.location.protocol;
          var sr_origin = '//' + host;
          var origin = protocol + sr_origin;
          $.ajax({
                         type:"POST",
                         url :origin+"/appName/post_view",
                         data:{
                                     "DATA_X": x,
                                     "DATA_Y" : y,
                                      csrfmiddlewaretoken:$("input[name=csrfmiddlewaretoken]").val() 
                                   },
            datatype:"json",
            error:function(data){ alert('Error:');},
            success:function(data){ alert("Success");}

          });
  • On the view side we need to provide a map for the URL : /appName/post_view mentioned above as : url (r'^post_view$',views.post_view,name='post_view') in the url.py file located within the appName folder [appName is the name of django app from which we want to make the call to view function]
Once this mapping is done we are good to make our ajax call ! 


1 comment:

  1. An expert with practical looks after contains executed a necessary character within the guide from the wrist chronograph and it is a chief in this problem. The actual corporation features shared the most effective minutes from the conquest within the skies for its robust, trustworthy and-performance tools. The planet exclusively key keep an eye on company to help you left arm it has the versions utilizing chronometer-licensed measures, one of the best expression regarding preciseness. But, not only a see, at the same time introduced quite a few treasured wall structure clocks. You will discover certain evaluations for the timepieces. The extra texts to the at any hour indices rarely intrude on the fast paced dial; a further reddish colored arrow give offers quickly indicator belonging to the hometime without delay. This operations hence does not affect typically the accurate of this see, nor to the efficiency of the chronograph. The fresh sit back and watch made due to this type comes from some others, absolutely constructed as well as made inside the provider work spaces replica cartier watches uk. This characteristics a similar primary buildings like a new column steering wheel, a fabulous directory coupling clutch system insuring high-precision account activation, in addition to a cutting-edge trademarked recentering product for your zero-resetting hammers. Whenever used with the chronograph replica breitling uk, this feature allows for an individual so that you can determine entail velocity long lasting time passed, the distance protected or even any velocity gotten to. Your sit back and watch may be very dependable as well as its leisure segments can easily be bought with all store. Like all other looks after, you can actually attain care in addition to standard company on this chronograph internationally www.waxwatchreplicas.co.uk. It is a enthusiast item not to mention suggestions about its operation together with thoughtful modernize exists in many countries. If you choose to, a eatches may well go down just as well, this particular bargain The holiday season Treasure is a wonderful suggestion convey in which prefers running watches. However those designer watches can be simple reproductions of vintage designs of your authentic hallmark units, they have been which means that the same with them who possibly even pros can be can not actually tell them away. That Designer watches are connected with excellent and are also designed choosing long-lasting metal. These appear in a nice-looking black colored color full of a watch accessory of which features crocodile natural leather.

    ReplyDelete