While working on an issue related to this, I wanted to test setting CSRF tokens for a bunch of endpoints that are accessible via AJAX. I came upon this thread and thought it might be worth it to add one other way to do it from the Django shell, which is what the original poster seemed to be asking about.
First of all, I am using the requests
library, so if you don't have it you have to do pip install requests
.
Ensure Token Exists
Next, if dealing only with Ajax endpoints, we need some way of retrieving a token, so the get method of your view must have the following decorator set, because as mentioned in the Django docs the token won't be set if you're generating forms dynamically. Here's a class-based-views example:
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import ensure_csrf_cookie
@method_decorator(ensure_csrf_cookie)
def get(self, request, *args, **kwargs):
return SomeJson...
DRF Post: Ensure csrf_protect
I am also using Django Rest Framework views, so I had to explicitly set csrf_protect
on my POST:
from django.views.decorators.csrf import csrf_protect
@method_decorator(ensure_csrf_cookie)
def post(self, request, *args, **kwargs):
return SomeJson...
Retrieve token from shell and POST it back
Finally, to get a token from the get
method and POST it back to the post
method, I did the following:
>>> resp = requests.get(url)
>>> cookies = resp.cookies
>>> token = cookies['csrftoken']
>>> data = {'content': True, 'we': None, 'are': False, 'posting': 0}
>>> data['csrfmiddlewaretoken'] = token
>>> resp2 = requests.post(url, data=data, cookies=cookies)
>>> resp2
<Response [200]>