SPA Django-Rest-Framework和React Issue:Not Found

时间:2018-04-20 17:30:39

标签: reactjs django-rest-framework single-page-application

我目前正在使用react和drf进行简单的单页应用。所以在drf我有APIView的post和get方法。我正在我的前端获取数据。获得请求一切顺利。但是我被邮寄了。

[20/Apr/2018 13:59:11] "GET /api/v1/update/ HTTP/1.1" 200 454
Not Found: /api/v1/update
[20/Apr/2018 13:59:11] "POST /api/v1/update HTTP/1.1" 404 2421`

请帮我找到错误

我的观点来了:

class TaskFormView(APIView):

def get(self, request, format=None):
    """
    Return a list of all tasks.
    """
    tasks = [{'name':task.name, 'status':task.status} for task in Task.objects.all()]
    return Response(tasks)


@csrf_exempt
def post(self, request, format=None):
    '''
    Post a new tasks
    '''
    serializer = TaskSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status.HTTP_201_CREATED)
    return Response(serializer.errors, status.HTTP_400_BAD_REQUEST)

从反应应用中获取:

  uploadTask() {
fetch("http://127.0.0.1:8000/api/v1/update", {
method: 'POST',
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
  name: "name",
  status: true
})})
.then(response => response.json())
.then(data => console.log('Data is ok. ', data))
.catch(err => console.log("parsing failed", err))
}

1 个答案:

答案 0 :(得分:0)

尝试在您的/请求中添加斜杠(POST)。

因此,POST的网址应为http://127.0.0.1:8000/api/v1/update/