9

How can I use the jquery.post() method in Django?

This is what I am trying to do:

         var postdata={
              'username':$('#login-email').val(), 
              'password':$('#login-password').val()
         }

         $.post('/login/',postdata)

How do I CSRF protect this in django? Is there a way to add to the CSRF token to the post data?

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
tallowen
  • 4,198
  • 7
  • 27
  • 35
  • I think you really mean to ask "How do I submit AJAX requests in Python", right? There's no "jquery.Post()" method in any other language or framework other than jquery. You're asking for the equivalent operation, correct? – Mike Atlas Mar 23 '11 at 15:22
  • 2
    @Mike I believe he's asking how to use jQuery to POST data *to* Django, I'll edit the title (again) to make it more clear – Daniel DiPaolo Mar 23 '11 at 15:26

4 Answers4

34

Yes. I believe it's stored in {{ csrf_token }}. So, just do

     var postdata={
          'username':$('#login-email').val(), 
          'password':$('#login-password').val(),
          'csrfmiddlewaretoken': '{{ csrf_token }}'
     }

You might have to double check the names, but that should be right.

mpen
  • 272,448
  • 266
  • 850
  • 1,236
8

I usually refer a file with this content to every page I want to be able to make AJAX requests:

if (!$)
    var $ = django.jQuery;

$('html').ajaxSend(function(event, xhr, settings) {
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = $.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
        // Only send the token to relative URLs i.e. locally.
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
});
vmassuchetto
  • 1,529
  • 1
  • 20
  • 44
5

Although you didn't provide your html in your example is it safe to assume that you are using a <form>? If so, add your CSRF token template tag to your form and call .serialize() on your form.

HurnsMobile
  • 4,341
  • 3
  • 27
  • 39
0

The contrib module in Django has a CSRF module that you can use.

As to your question how to send a POST, as long as you have the URL mapped properly requests will get sent to it. You can handle a POST request specifically by checking request.POST on the request object.

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
  • I specifically was talking about the $.post method. The reason I asked this question is that I did not want to have to deal with such a cumbersome script. – tallowen Mar 23 '11 at 15:27