我试图在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(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)
答案 0 :(得分:1)
SearchCityListCreate
的网址格式与/city/x/
匹配,因此您的请求正在由错误的视图处理。
您通过切换订单来解决问题,但更好的解决方法是确保您的正则表达式分别标记了URL的开头和结尾^
和$
。
url(r'^city$', SearchCityListCreate.as_view()),
url(r'^city/(?P<pk>[0-9]+)$',SearchCityDetail.as_view()),
答案 1 :(得分: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
答案 2 :(得分:0)
您可以使用rest_framework类视图`country_detail(APIView)类来实现它: def get_object(self,pk): 尝试: 返回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()`