500 внутренняя ошибка сервера при использовании Django и React

Я столкнулся с некоторой проблемой при использовании react и django.

Когда я сделал http запрос к серверу django, я получил 500 Internal Server Error.

Мой код выглядит следующим образом.

urls.py

from usercontrol.views import *

urlpatterns = [
    path('admin/', admin.site.urls),
    path('auth/', UserView.as_view(), name="something")
]

usercontrol/views.py

from http.client import HTTPResponse
from django.shortcuts import render
from rest_framework.views import APIView
from . models import *
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated, AllowAny


# Create your views here.


class UserView(APIView):
    permission_classes = [AllowAny]
    def get(self, request):
        if request.user.is_authenticated:
            print ("HELO")
        return HTTPResponse("Hello")
    def post(self, request):
        return HTTPResponse("Hello")
React Code
axios.get("http://localhost:8000/auth/")
 .then((res) => {
  console.log(res);
 })
 .catch((err) => {});

Почему я получаю 500 Internal Server Error? Пожалуйста, помогите мне исправить эту ошибку.

Необходимо установить corsheaders в django.

pip install django-cors-headers

Также необходимо добавить следующую конфигурацию в settings.py и добавить corsheaders в Installed_apps

CORS_ORIGIN_ALLOW_ALL = True

CORS_ORIGIN_WHITELIST = [
    "http://127.0.0.1:3000", 
    "http://127.0.0.1", 
    "http://localhost:3000", 
    "http://localhost"
]

ALLOWED_HOSTS = ['*', 
    "http://127.0.0.1:3000", 
    "http://127.0.0.1", 
    "http://localhost:3000", 
    "http://localhost"
]

Во время работы сервера react убедитесь, что сервер backend django также запущен.

Вернуться на верх