首页 > 解决方案 > Django REST Framework 删除方法请求收到 Forbidden 403

问题描述

视图.py

class DeviceView(viewsets.ModelViewSet):

serializer_class = DevicesSerializer
queryset = Devices.objects.all() 
permission_classes = [permissions.IsAuthenticated]

Axios 请求

    axios.delete(`api/devices/${data.id}/`, {}, {
    headers: {'X-CSRFToken': csrftoken }
})
    .then((response) => {
        console.log(response);
    }).catch((error) => {
        console.log(error);
    });

当我在我的前端执行这个请求时,我得到一个响应:“DELETE http://localhost:3000/api/devices/4/ 403 (Forbidden)”。其中 4 是属于我要删除的记录的 ID。

我目前在 Django 中使用会话身份验证,并且我已将我的 CSRF 令牌值传递到我的请求标头中。

当我在表单上使用 PUT 和 POST 等其他方法时,它们可以正常工作。但是,不是删除

收到此错误我做错了什么?

标签: djangoajaxdjango-rest-frameworkdjango-viewsaxios

解决方案


原来我只需要删除 Axios 请求中的空正文。

axios.delete(`api/devices/${data.id}/`, {
    headers: {'X-CSRFToken': csrftoken }
})
    .then((response) => {
        console.log(response);
    }).catch((error) => {
        console.log(error);
    });

推荐阅读