put 方法不适用于 RetrieveUpdateDestroyAPIView Django Rest Framework Angular
put method not working on RetrieveUpdateDestroyAPIView Django Rest Framework Angular
我正在尝试在 Django 休息框架中发出放置请求。我的视图继承自 RetrieveUpdateDestroyAPIView class。
我在前端使用 angular 在后端使用 django rest。
这是错误:
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);
正在映射的 url django 端:
url(r'^city/(?P<pk>[0-9]+)',SearchCityDetail.as_view())
风景class
class SearchCityDetail(RetrieveUpdateDestroyAPIView):
queryset = SearchCity.objects.all()
serializer_class = SearchCitySerializer
RetrieveUPdateDestoryAPIView 文档:
http://www.django-rest-framework.org/api-guide/generic-views/#updatemodelmixin
检索UpdateDestroyAPIView
用于读写删除端点以表示单个模型实例。
提供 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)
您 SearchCityListCreate
的 URL 模式匹配 /city/x/
因此您的请求被错误的视图处理。
您通过切换顺序解决了问题,但更好的解决方法是确保您的正则表达式具有 ^
和 $
分别标记 URL 的开头和结尾.
url(r'^city$', SearchCityListCreate.as_view()),
url(r'^city/(?P<pk>[0-9]+)$',SearchCityDetail.as_view()),
我需要颠倒我所在城市的顺序urls
原来如此,pk的城市url从未被占领过。
不好:
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
您可以使用 rest_framework class 视图`class country_detail(APIView) 来实现它:
def get_object(self,pk):
尝试:
return 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()`
我正在尝试在 Django 休息框架中发出放置请求。我的视图继承自 RetrieveUpdateDestroyAPIView class。
我在前端使用 angular 在后端使用 django rest。
这是错误:
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);
正在映射的 url django 端:
url(r'^city/(?P<pk>[0-9]+)',SearchCityDetail.as_view())
风景class
class SearchCityDetail(RetrieveUpdateDestroyAPIView):
queryset = SearchCity.objects.all()
serializer_class = SearchCitySerializer
RetrieveUPdateDestoryAPIView 文档:
http://www.django-rest-framework.org/api-guide/generic-views/#updatemodelmixin
检索UpdateDestroyAPIView 用于读写删除端点以表示单个模型实例。
提供 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)
您 SearchCityListCreate
的 URL 模式匹配 /city/x/
因此您的请求被错误的视图处理。
您通过切换顺序解决了问题,但更好的解决方法是确保您的正则表达式具有 ^
和 $
分别标记 URL 的开头和结尾.
url(r'^city$', SearchCityListCreate.as_view()),
url(r'^city/(?P<pk>[0-9]+)$',SearchCityDetail.as_view()),
我需要颠倒我所在城市的顺序urls
原来如此,pk的城市url从未被占领过。
不好:
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
您可以使用 rest_framework class 视图`class country_detail(APIView) 来实现它: def get_object(self,pk): 尝试: return 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()`