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
请求传递的其他数据。
`