1👍
In addition to @ArpitGoyal’s answer you can also decorate your view with csrf_exempt
:
This decorator marks a view as being exempt from the protection ensured by the middleware.
A few tips in case you do need CSRF protection:
-
Check CSRF token cookie name.
See
CSRF_COOKIE_NAME
for more information. -
Add
ensure_csrf_cookie
decorator to your view.According to the docs:
Warning
If your view is not rendering a template containing the
csrf_token
template tag, Django might not set the CSRF token cookie. This is common in cases where forms are dynamically added to the page. To address this case, Django provides a view decorator which forces setting of the cookie:ensure_csrf_cookie()
. -
Assuming that CSRF token cookie name is
csrftoken
, try to sendX-CSRFToken
header.$.ajax({ // Your options here. headers: {'X-CSRFToken': getCookie('csrftoken')} });
1👍
You should authenticate your client before making the request. From your call you are providing a ajax POST
request hit.
Provide a header
in your RESTClient
: X-CSRFToken
.
For more details view this