I'm new to React and I try to make Django Rest and react work together.
Btw, my javascript is enabled.
I have a simple view :
class StudentView(generics.ListCreateAPIView):
queryset = Student.objects.all()
serializer_class = StudentSerializer
I try to fetch it from react with :
useEffect(() =>{
fetch("http://127.0.0.1:8000/secretariat/get_student/", {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
})
.then( resp => resp.json())
.then( resp => setStudents(resp))
.catch(error => console.log(error))
}, [])
When i check in browser network the response this is what i have :
I dont think that i have a CORS Header problem but here my CORS settings.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'corsheaders',
'common_app',
'rest_framework',
'rest_framework.authtoken',
'allauth.account',
'rest_auth.registration',
'ldap',
'rest_auth',
'simple_history',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'simple_history.middleware.HistoryRequestMiddleware',
]
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (
'http://127.0.0.1'
)
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',
)
I assume i'm doing something wrong but i have no clue.
Thanks