覆盖“ perform_destroy”时,获取{“ detail”:“不允许使用方法\” POST \“。”}

时间:2019-03-14 06:43:30

标签: django django-rest-framework django-urls http-status-code-405

Django的新手,并尝试实现REST API。 这是代码:

from views.py

class reg_event(generics.CreateAPIView):
     queryset = event_registration_stub.objects.all()
     serializer_class = serializers.EventRegistrationStubSerializer

     def perform_create(self, serializer):
         serializer.save(parent_volunteer=self.request.user)


class reg_event_delete(generics.DestroyAPIView):
     queryset = event_registration_stub.objects.all()
     serializer_class = serializers.EventRegistrationStubSerializer

def perform_destroy(self, serializer):
    instance.delete(parent_volunteer=self.request.user, parent_event=self.kwargs["parent_event"])

这是序列化器:

class  EventRegistrationStubSerializer(serializers.ModelSerializer):
      parent_volunteer = serializers.ReadOnlyField(source='parent_volunteer.id')
     class Meta:
         model = event_registration_stub
         fields = ('id', 'parent_event', 'parent_volunteer')

从urls.py

    url(r'^register_user_for_event/$', views.reg_event.as_view(), name='register_user_for_event'),
    url(r'^delete_user_for_event/$', views.reg_event_delete.as_view(), name='delete_user_for_event'),

注册用户以进行活动。但是,以下内容:

   curl -X POST -d "parent_event=1" -H 'Authorization: Token 21cc749c43bf80f27598987cd5a9926d988ba64c' http://127.0.0.1:8000/api/delete_user_for_event/

返回

"POST /api/delete_user_for_event/ HTTP/1.1" 405 41

我尝试查看405 POST method not allowed

但是我不确定我的网址出了什么问题。我的表演破坏不正确吗?任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果要发送带有卷曲的destroy请求,则应使用:

curl -X DELETE ...

generics.DestroyAPIView仅具有删除方法。这就是为什么发送发帖请求时出现405错误