I am trying to make an API GET request from a React & Redux app to a Django app, but am getting an error about CORS with a 301 response. Below is the response
Here is the React & Redux code:
const fetchClasses = createAsyncThunk(
GET_CLASSES,
async (id) => {
const CLASSES_API = `http://127.0.0.1:8000/learning/classes?schoolPk=${id}`;
const response = await fetch(CLASSES_API);
const data = await response.json();
return data;
},
);
When I use the same code to fetch all schools, it works. But when I add the id params to get all classes in a certain school, that's when I get the error. Below is the views.py code in Django.
# Create your views here.
class SchoolViewSet(viewsets.ModelViewSet):
queryset = School.objects.all()
serializer_class = SchoolSerializer
class ClassViewSet(viewsets.ModelViewSet):
serializer_class = ClassSerializer
def get_queryset(self):
return Class.objects.filter(school=self.request.query_params['schoolPk'])
I have tried setting CORS in Django settings.py file.
ALLOWED_HOSTS = ['http://localhost:3000/', '127.0.0.1',]
CORS_ALLOWED_ORIGINS = ['http://localhost:3000',]
CORS_ORIGIN_WHITELIST = (
'http://localhost:3000',
)
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_METHODS = [
"DELETE",
"GET",
"OPTIONS",
"PATCH",
"POST",
"PUT",
]
CORS_ALLOW_HEADERS = [
"accept",
"accept-encoding",
"authorization",
"content-type",
"dnt",
"origin",
"user-agent",
"x-csrftoken",
"x-requested-with",
]
CORS_EXPOSE_HEADERS = [
"accept",
"accept-encoding",
"authorization",
"content-type",
]
CORS_ALLOW_CREDENTIALS = True
I am thinking I could be missing something in React because every request is working perfectly in Postman
What could I be missing please?