【问题标题】:Django REST Framework Delete Method Request receives Forbidden 403Django REST Framework 删除方法请求收到 Forbidden 403
【发布时间】:2021-10-05 00:04:01
【问题描述】:

views.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 等其他方法时,它们可以正常工作。但是,不是删除

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

【问题讨论】:

  • 只是为了确认您在 Django 开发服务器上遇到了这个问题?
  • 检查问题出在哪里的一种方法是在此删除 url 上使用 curl 命令。如果可以,它可能与您的 ajax 调用有关,如果不是,则可能与您的 api 有关。
  • 也许模型上的 delete.CASCADE 可能是问题所在。

标签: django ajax django-rest-framework django-views axios


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-13
    • 2020-08-29
    • 2018-09-22
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2019-11-22
    相关资源
    最近更新 更多