Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Django RestFramework中处理PUT请求_Python_Django_Api_Django Rest Framework - Fatal编程技术网

Python 如何在Django RestFramework中处理PUT请求

Python 如何在Django RestFramework中处理PUT请求,python,django,api,django-rest-framework,Python,Django,Api,Django Rest Framework,我正试图使用PUT api更新“VoterList”模型中的数据,但我不知道应该在“views.py”文件中使用哪个函数来处理即将到来的PUT请求,因为在PUT api中,我们使用URL中的参数从模型中选取相关条目进行更新,然后使用从PUT api接收的数据进行更新 model.py class VoterList(models.Model): # id = models.IntegerField(auto_created= True, primary_key=True) nam

我正试图使用PUT api更新“VoterList”模型中的数据,但我不知道应该在“views.py”文件中使用哪个函数来处理即将到来的PUT请求,因为在PUT api中,我们使用URL中的参数从模型中选取相关条目进行更新,然后使用从PUT api接收的数据进行更新

model.py

class VoterList(models.Model):
    # id = models.IntegerField(auto_created= True, primary_key=True)
    name = models.CharField( max_length=20)
    email = models.EmailField()
    mobile = models.IntegerField()
    city = models.CharField( max_length=20)
    type = models.CharField(max_length=20)

    def __str__(self):
        return self.name
序列化程序.py

class FillVoterListSerializers(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = VoterList
        fields = ('id','name', 'email', 'mobile', 'city', 'type')

    def update(self, instance, validated_data):
        instance.name = validated_data.pop("name", instance.name)
        instance.email = validated_data.pop("email", instance.email)
        instance.save()

        return  instance
我将自己管理放入序列化程序的代码

views.py

class UpdateVoter(APIView):

    serializer_class =  FillVoterListSerializers
    permission_classes = (AllowAny,)

    def post(self, request,*args,**kwargs):

        isDataExist =  VoterList.objects.get(id=request.data.get('id'))

        if not isDataExist:
            return Response({"message":"No Voter exist with this id."})
        else:
            isDataUpdated =  self.serializer_class(isDataExist, request.data, partial=True)

            if isDataUpdated.is_valid():
                isDataUpdated.save()
                return Response({"message": "Voter updated."})
            else:
                return  Response({"message": "All fields are Mandatory."})
url.py

 urlpatterns = [
  url('api/updateVoter/(?P<id>[0-9]+)/$', UpdateVoter.as_view(), name= "updateVoter")]
urlpatterns=[
url('api/updateVoter/(?P[0-9]+)/$',updateVoter.as_view(),name=“updateVoter”)]
那么,我应该在view.py中编写什么代码来处理PUT请求呢

注意:我想告诉你们,我正在为移动应用程序准备api,所以请做出相应的回应

非常感谢您的帮助。

您可以在视图中使用与您使用的post()类似的put()函数

def put(self, request, pk, format=None):
     # Your code here
参考DRF文档:

您可以在视图中使用put()函数,类似于您使用的post()

def put(self, request, pk, format=None):
     # Your code here

请参阅DRF文档:

您使用
ApiVew
中的
put
方法。如果您定期发布带有特定变量的帖子,您可以在
请求
对象中找到该变量,并使用该变量区分发送位置(如您自己的put方法)。@dan klasson-感谢您的回复。但你的回答是我所问问题的另一个选择。我已经尝试了你提供的解决方案,而且它也起了作用。但是如果我们通过PUT请求,我正在寻找方法。请看我的第一句话。您使用
ApiVew
中的
PUT
方法。如果您定期发布带有特定变量的帖子,您可以在
请求
对象中找到该变量,并使用该变量区分发送位置(如您自己的put方法)。@dan klasson-感谢您的回复。但你的回答是我所问问题的另一个选择。我已经尝试了你提供的解决方案,而且它也起了作用。但我正在寻找方法,如果我们通过PUT请求。请看我的第一句话。