put方法不适用于RetrieveUpdatedStroyapiView Django Rest框架

put方法不适用于RetrieveUpdatedStroyapiView Django Rest框架,django,django-rest-framework,put,http-error,django-generic-views,Django,Django Rest Framework,Put,Http Error,Django Generic Views,我试图在django rest框架中发出put请求。我的视图是从RetrieveUpdatedStroyapiView类继承的 我使用的前端django休息的后端角度 以下是错误: Failed to load resource: the server responded with a status of 405 (Method Not Allowed) ERROR detail:"Method "PUT" not allowed." 下面是从angular端到django rest的put

我试图在django rest框架中发出put请求。我的视图是从RetrieveUpdatedStroyapiView类继承的

我使用的前端django休息的后端角度

以下是错误:

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
ERROR 
detail:"Method "PUT" not allowed."
下面是从angular端到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())
RetrieveUpdatedStoryApiView文档:

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

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

扩展:GenericAPIView、RetrieveModelMixin、UpdateModelMixin、DestroyModelMixin

RetrieveUpdatedStroyapiView源代码:

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)

您的
SearchCityListCreate
的URL模式与
/city/x/
匹配,因此您的请求被错误的视图处理

您通过切换顺序解决了这个问题,但更好的解决方法是确保您的正则表达式有
^
$
分别标记URL的开始和结束

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

我需要颠倒城市URL的顺序

事实上,带有pk的城市url从未被使用过

坏的:

url(r'city',SearchCityListCreate.as_view()),#创建城市列表url
url(r'city/(?P[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
url(r'city/(?P[0-9]+)/$”,SearchCityDetail.as_view(),
url(r'city',SearchCityListCreate.as_view()),35;创建城市列表url

您可以使用rest\u框架类视图`类国家/地区详细信息(APIView)来实现它: def get_对象(自身,主键): 尝试: 返回CountryModel.objects.get(pk=pk) 除CountryModel.DoesNotExist外: 提高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()` 

也许您有一个冲突的URL模式,请求没有被
SearchCityDetail
@Alasdair处理,这是一个很好的想法,我将对此进行研究it@Alasdair所以这两个url模式以相反的方式出现在现在的位置。我遇到了一个连接被拒绝的问题,但我会解决这个问题,我认为我们可能会很好地
url(r'city/(?P[0-9]+)/$),SearchCityDetail.as#view())\detele put get url url(r'city',SearchCityListCreate.as#view()),#创建城市列表url
@Alasdair。它起作用了Hanks@Alasdair我现在就做这些改变
 url(r'city/(?P<pk>[0-9]+)/$',SearchCityDetail.as_view()), 
 url(r'city', SearchCityListCreate.as_view()), # create city list url
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()`