Django rest框架在执行ajax post调用时csrf豁免

Django rest框架在执行ajax post调用时csrf豁免,django,django-rest-framework,Django,Django Rest Framework,我试图通过ajaxpost请求传递数据,但得到了403错误 我曾尝试从大括号中使用CsrfExemptMixin。视图,但这并不能解决问题 我不熟悉使用drf的api调用,所以我的方法可能不正确 查看 class DialogListView(CsrfExemptMixin, viewsets.ModelViewSet): # queryset = Dialog.objects.all() serializer_class = DialogSerializer # ht

我试图通过
ajaxpost
请求传递数据,但得到了
403错误

我曾尝试从
大括号中使用
CsrfExemptMixin
。视图
,但这并不能解决问题

我不熟悉使用
drf
的api调用,所以我的方法可能不正确

查看

class DialogListView(CsrfExemptMixin, viewsets.ModelViewSet):
    # queryset = Dialog.objects.all()
    serializer_class = DialogSerializer

    # http_method_names = ['get', 'delete']


    def get_queryset(self):
        data = self.request.data
        print('helllo')
        print(self.request.data)
        # for k in data:
        #     print(k)
        try:
            owner_profile = Profile.objects.get(user=User.objects.get(username=data.get('owner')))
            opponent_profile = Profile.objects.get(user=User.objects.get(username=data.get('opponent')))
            return Dialog.objects.get(Q(owner=owner_profile,
                                        opponent=opponent_profile) |
                                      Q(owner=opponent_profile,
                                        opponent=owner_profile))

        except ObjectDoesNotExist:
            owner_profile = Profile.objects.get(user=User.objects.get(username=data.get('owner')))
            opponent_profile = Profile.objects.get(user=User.objects.get(username=data.get('opponent')))
            return Dialog.objects.create(owner=owner_profile,
                                         opponent=opponent_profile)

不管怎样,我想出来了。我已将
csrfmiddlewaretoken:{{csrf_token}}}
添加到
数据
键中,其中包含通过
POST
请求传递的其他数据。
`

不管怎么说,我想出来了。我已将
csrfmiddlewaretoken:{{csrf_token}}}
添加到
数据
键中,其中包含通过
POST
请求传递的其他数据。 `