put方法不适用于RetrieveUpdateDestroyAPIView Django Rest Framework Angular

问题描述 投票:1回答:3

我试图在django rest框架中发出put请求。我的观点是继承自RetrieveUpdateDestroyAPIView类。

我在前端django上使用角度在后端休息。

这是错误:

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
ERROR 
detail:"Method "PUT" not allowed."

这是从角度到django rest的put请求的完整实现

editcity(index){
    this.oldcityname = this.cities[index].city;
     const payload = {
      citypk: this.cities[index].pk,
      cityname: this.editcityform.form.value.editcityinput
    };
     this.suitsettingsservice.editcity(payload, payload.citypk)
       .subscribe(
         (req: any)=>{
           this.cities[index].city = req.city;
           this.editcitysucess = true;
           // will have changed
           this.newcityname = this.cities[index].city;
         }
       );
  }

正在调用的服务

editcity(body, pk){
    const url = suitsettingscity + '/' + pk;
    return this.http.put(url, body);

正在映射django端的url:

url(r'^city/(?P<pk>[0-9]+)',SearchCityDetail.as_view())

视图类

class SearchCityDetail(RetrieveUpdateDestroyAPIView):
    queryset = SearchCity.objects.all()
    serializer_class = SearchCitySerializer

RetrieveUPdateDestoryAPIView文档:

http://www.django-rest-framework.org/api-guide/generic-views/#updatemodelmixin

RetrieveUpdateDestroyAPIView用于读写 - 删除端点以表示单个模型实例。

提供get,put,patch和delete方法处理程序。

扩展:GenericAPIView,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin

RetrieveUpdateDestroyAPIView源代码:

class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
                                   mixins.UpdateModelMixin,
                                   mixins.DestroyModelMixin,
                                   GenericAPIView):
    """
    Concrete view for retrieving, updating or deleting a model instance.
    """
    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def patch(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)
django django-rest-framework put http-error django-generic-views
3个回答
1
投票

您的SearchCityListCreate的网址格式与/city/x/匹配,因此您的请求是由错误的视图处理的。

您通过切换顺序来解决问题,但更好的解决方法是确保您的正则表达式分别使用^$来标记URL的开头和结尾。

url(r'^city$', SearchCityListCreate.as_view()),
url(r'^city/(?P<pk>[0-9]+)$',SearchCityDetail.as_view()),

0
投票

我需要改变我的城市网址的顺序

就这样,带有PK的城市网址从未被采用过。

坏:

url(r'city', SearchCityListCreate.as_view()), # create city list url
url(r'city/(?P<pk>[0-9]+)/$',SearchCityDetail.as_view()), 

好:

 url(r'city/(?P<pk>[0-9]+)/$',SearchCityDetail.as_view()), 
 url(r'city', SearchCityListCreate.as_view()), # create city list url

0
投票

你可以使用rest_framework类视图`class country_detail(APIView)来实现它:def get_object(self,pk):try:return CountryModel.objects.get(pk = pk),CountryModel.DoesNotExist除外:raise Http404

def get(self,request,pk,format=None):
    country=self.get_object(pk)
    serializer=CountrySerializer(country)
    return Response(serializer.data,status=status.HTTP_200_OK)
def put(self,request,pk,format=None):
    country=self.get_object(pk)
    serializer=CountrySerializer(country,data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data,status=status.HTTP_200_OK)
    return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
def delete(self,request,pk,format=None):
    country=self.get_object(pk)
    country.delete()` 
© www.soinside.com 2019 - 2024. All rights reserved.