Reputation: 58863
I'm trying to make an Ajax login form. I can get the user authenticated with django.contrib.auth.authenticate
by passing it the username and password coming from the ajax post. But how can I them set an actual session and csrf so that in subsequent calls I can get the user in request.user?
Thanks
Upvotes: 3
Views: 1440
Reputation: 8354
Add {% csrf_token %}
as normal then add a pre-request callback function beforeSend on your AJAX POST etc:
beforeSend: function (request) {
request.setRequestHeader("X-CSRFToken",
$('input[name="csrfmiddlewaretoken"]').val());
},
If using an API Framework like TastyPie you have other options for authentication. See here: How can I login to django using tastypie
Upvotes: 4