title: How do I POST with jQuery/Ajax in Django?
body: <p>I am trying to POST data using jQuery/AJAX in Django and am having trouble.  When I run the code below and click on the 'test' button, the entire page re-loads again, which isn't what I want to happen (that's why I'm using AJAX).</p>
  
  <p>I'm also not able to confirm the AJAX request is getting to the Django view.</p>
  
  <p><strong>EDIT:</strong> I've made the edits for return false and <code>event.preventDefault()</code>.  A new page doesn't load, but I'm still not able to see the updated text in the <code>&lt;div id='message'&gt;</code> field. I'm not sure if the data is getting sent. I'm seeing in the console:</p>
  
  <pre><code>POST /edit_favorites/ HTTP/1.1" 403 2294
  </code></pre>
  
  <p><strong>views.py</strong>:</p>
  
  <pre><code>from django.shortcuts import render_to_response
  from django.http import HttpResponse
  
  def edit_favorites(request):
      if request.is_ajax():
          message = "Yes, AJAX!"
      else:
          message = "Not Ajax"
      return HttpResponse(message)
  </code></pre>
  
  <p><strong>urls.py</strong>:</p>
  
  <pre><code>url(r'^edit_favorites/', 'edit_favorites'),
  </code></pre>
  
  <p><strong>HTML</strong>:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;form method='post' id='test'&gt;
    &lt;input type="hidden" value="video34"/&gt;
    &lt;input type='submit' value='Test button'/&gt;
    &lt;div id='message'&gt;Initial text&lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>JavaScript</strong>:</p>
  
  <pre class="lang-js prettyprint-override"><code>$(document).ready(function () {
    $("#test").submit(function (event) {
      event.preventDefault();
      $.ajax({
        type: "POST",
        url: "/edit_favorites/",
        data: {
          'video': $('#test').val() // from form
        },
        success: function () {
          $('#message').html("&lt;h2&gt;Contact Form Submitted!&lt;/h2&gt;")
        }
      });
      return false;
    });
  });
  </code></pre>
  
  <p>Any advice?</p>