Django and AngularJS already have CSRF support, your part is pretty simple.
First, you need to enable CSRF in Django, I suppose you've already done this, if not, follow the Django doc https://docs.djangoproject.com/en/1.5/ref/contrib/csrf/#ajax .
Now Django will set a cookie named csrftoken in the first GET request and expect a custom X-CSRFToken HTTP header in subsequent POST / PUT / DELETE requests.
For Angular, it expects a cookie called XSRF-TOKEN and will make POST / PUT / DELETE requests with the X-XSRF-TOKEN , so you need to adjust it a bit so that both of them are with each other:
$httpProvider.defaults.xsrfCookieName = 'csrftoken'; $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
Add the above two lines somewhere in your js code, the module.config () block is a good place to do this.
What is it.
NOTE. This is for angular 1.1.5, older versions may require a different approach.
Update:
Since the angular application is not served by django in order to allow the cookie to be set, the angular application must first execute a GET request for django.
Ye Liu Aug 09 '13 at 22:45 2013-08-09 22:45
source share