title: Django/JQuery CSRF: 403 Error
body: <p>I keep getting 403 Errors from Django.</p>
  
  <p>I set up my settings.py to use the CSRF protection, and used the csrf_token token in my template.</p>
  
  <p>Here is the JS file I included just after the HTML header: <a href="http://bpaste.net/show/87791/" rel="nofollow">http://bpaste.net/show/87791/</a></p>
  
  <p>Using Firebug I can check that the CSRF cookie is there. Later on the page, the user clicks on a button that triggers this code:</p>
  
  <pre><code>myFunction: function() {
    $.ajax({
    type: 'POST',
    url: window.localtion.href + 'myajaxview',
    async: false
    });
  }
  </code></pre>
  
  <p>I am using a simple class based view inheriting from TemplateView to display this page. 'myajaxview' is inheriting from View and a JSON Mixin. However its code is never executed since django cannot validate the CSRF token.</p>
  
  <p>It seems to me that the ajax doesn't send the token with the POST headers as it should. Or am I missing something?</p>
  
  <p><strong>EDIT:</strong> I moved the $.ajaxSetup call just before the call to the $.ajax() POST function and it worked. I tried to move it somewhere else and it failed. The problem is more related to Ajax than Django I think.
  So, my question is still there, I don't want to put the $.ajaxSetup call before each $.ajax call, I don't think it is the way things are done, I don't want to repeat myself. So this was just a workaround and I am asking for the solution.</p>