使用vue.js请求时,Django RetrieveDestroyAPIView无法正常工作

时间:2018-07-12 06:36:54

标签: django django-rest-framework vue-cli

我创建了一个Django rest API,并通过vue.js函数实现了按给定ID删除对象的功能,单击按钮后,我实现了一个函数,将具有ID给ID的URL传递给API以删除该对象。宾语。 Django rest API可在其Rest API网站中使用,但是通过vue.js函数访问API时,它并没有删除对象,甚至也没有错误显示

#HTML代码

<td v-on:click="deleteCountry(c.id)"><center><p class="fas fa-times"></p></center></td> // c.id is is dynamic id i am getting by django db

#vue.js代码

<script>
export default {
  data () {
    return {
    title:'app',
},
 methods:{
deleteCountry(id){
      this.$http.get('http://127.0.0.1:8000/api/countries/'+id+'/delete/');
    },
}
 }
}
</script>

django rest api视图集代码

from rest_framework .generics import(
    ListAPIView,
    RetrieveAPIView,
    DestroyAPIView,
    UpdateAPIView,
    RetrieveDestroyAPIView,
    RetrieveUpdateAPIView)

class CountryDeleteAPIView(RetrieveDestroyAPIView):
    queryset = countries.objects.all()
    serializer_class = CountryDetailSerializer
    lookup_field = "id"

1 个答案:

答案 0 :(得分:0)

您正在使用HTTP GET进行删除。因此,请更改

this.$http.get('http://127.0.0.1:8000/api/countries/'+id+'/delete/');

this.$http.delete('http://127.0.0.1:8000/api/countries/'+id+'/delete/');