'CSRF cookies not set - React, JWT, Django

I'm rather confused regarding the following error: "Forbidden (CSRF cookie not set.)". This error is received during attempting to logout, login, signup.

The problem is similar to this post which was never answered: Django (DRF) & React - Forbidden (CSRF cookie not set)

I used axios and JWT for handling authentication. I have two git branches to track this error. On the master branch I have the original authentication setup. It works just fine, no errors. On a second branch (we'll call it branch2), I get the error specified above. The only difference between the two branches is that I ran the cmd django-admin startapp books. I then proceeded to setup the model, serialization, views, and urls for the books app on branch2. I also added it to the settings.py installed apps. Other than that, nothing has changed. Therefore the authentication process should remain the same.

React handles the looks of the website but the default django ip is used for development: http://127.0.0.1:8000/ I run npm run build in order to update react's current build.

Book View (branch2)

# Book Imports
from .serializers import BookSerializer
from .models import BookModel

#####   Book API  #####

# A Complete List of ALL Projects
class ProjectListAPI(generics.ListAPIView):
    queryset = BookModel.objects.all()
    serializer_class = BookSerializer

Book Serialization

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = BookModel
        fields = "__all__"

Book Urls

from .views import (BookListAPI, BookRetrieveUpdateApi)

urlpatterns = [
    path('book/list', BookListAPI.as_view(), name='book-list'),
]

While there are many similar issues posted on stack overflow regarding a CSRF cookie not set error, none seem to fix the error I am experiancing.



Solution 1:[1]

I just experienced situation where some PUT routes of my app worked, while others showed this csrf not set error. I found that if the pk was put at end of endpoint in my case it could only be a GET request. This fixed it. This finding is anecdotal. I switched from /comment/edit/ to /comment//edit

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 trier