title: Django: can't trigger AJAX call and send values back to my form
body: <p>I need to run an AJAX call to perform a quick calculation in my django view and return the result in my html page, inside a  tag.</p>
  
  <p>I'm very new to Javascript so I don't understand why my AJAX call hasn't been triggered. This is my html and JS code:</p>
  
  <pre><code>&lt;input type="text" name="SHm2" maxlength="10" type="number" value="50"&gt;
  &lt;input type="text" name="STm2" maxlength="10" type="number" value="50"&gt;
  
  &lt;button id="estimation" name= "estimation" onclick="calculate()"&gt;Estimation&lt;/button&gt;
  &lt;span&gt;{{estimation}}&lt;/span&gt;
  
  &lt;script type="text/javascript"&gt;
    function calculate () {
      $.ajax({
              url: '/myApp/templates/homepage/',
              type: 'POST',
              data: {
                SHm2: $('#SHm2').val(),
                STm2: $('#STm2').val()
              },
              success: function(estimation) {
              alert(estimation);
              document.getElementById("estimation").innerHTML = estimation;
  
              }
      }); 
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>And this is my views.py:</p>
  
  <pre><code>def homepage(request):
      if request.method == 'POST' and request.is_ajax and 'estimation' in request.POST:
          SHm2 = request.POST.get('SHm2')
          STm2 = request.POST.get('STm2')
          estimation = float(SHm2) + float(STm2)
          estimation = json.dumps(estimation)
          return HttpResponse(estimation, content_type='application/json')
  </code></pre>
  
  <p>The problem is that the AJAX code isn't triggered since I don't receive the alert. Nevertheless, the code in my django view is running anyway (which is strange, since I specified to run if 'request.is_ajax', which doesn't seem to be recognized on the other hand). It loads a new page where it correctly displays the result. But it's not what I want since I need the result to be in my form within the span tag where {{estimation}} is my variable.</p>
  
  <p>Could you please tell me what I'm doing wrong? Thanks!</p>
  
  <p>UPDATE:
  Thanks to your answers, it's getting better. I've replaced in views.py 'request.is_ajax' by 'request.is_ajax()'. I've added the 'id' attribute to my input boxes. This helped me to trigger the AJAX call and not to load stuff in a new page. There is one last thing though. I'm still not able to display in my span tag the value of the estimation variable. I realised that it had no 'id' attribute so I did the following change:</p>
  
  <pre><code>&lt;span id="estimation2"&gt;{{estimation}}&lt;/span&gt;
  </code></pre>
  
  <p>Also in my JS code, I replaced in the success part the last line to:</p>
  
  <pre><code>document.getElementById("estimation2").innerHTML = estimation;
  </code></pre>
  
  <p>Basically I replaced "estimation" by "estimation2".</p>
  
  <p>Unfortunately the span tag is not updated. Any idea what I am missing?</p>