[Django]-Django React Axios

7👍

Managed to fix it by changing the url (url:'/en/api/endpoint/') I was posting to, because apparently for a POST request:

You called this URL via POST, but the URL doesn’t end in a slash and you have APPEND_SLASH set. Django can’t redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/en/api/endpoint/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings

After that I started getting Forbidden 403, but by adding:

from django.views.decorators.csrf import csrf_protect
from django.utils.decorators import method_decorator

@method_decorator(csrf_protect)
def post(self, request):
    return Response()

and also changed the defaults in JS to:

axios.defaults.xsrfHeaderName = "X-CSRFToken";
axios.defaults.xsrfCookieName = "csrftoken";

and removed CSRF_COOKIE_NAME = "XCSRF-Token" from settings.py.

It worked.

Hope this helps somebody in the future.

👤VnC

Leave a comment