Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.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 Tastypie添加内容长度标题_Python_Django_Rest_Httpresponse_Tastypie - Fatal编程技术网

Python Django Tastypie添加内容长度标题

Python Django Tastypie添加内容长度标题,python,django,rest,httpresponse,tastypie,Python,Django,Rest,Httpresponse,Tastypie,我是Django开发的新手。我有一个资源和模型: 型号 class Player(models.Model): pseudo = models.CharField(max_length=32, unique=True) class PlayerResource(ModelResource): class Meta: queryset = Player.objects.all() resource_name = 'player' a

我是Django开发的新手。我有一个资源和模型:

型号

class Player(models.Model):
    pseudo = models.CharField(max_length=32, unique=True)
class PlayerResource(ModelResource):
    class Meta:
        queryset = Player.objects.all()
        resource_name = 'player'
        authentication = BasicAuthentication()
        authorization = Authorization()
        serializer = Serializer(formats=['xml'])
        filtering = {
            'pseudo': ALL,
        }
模型资源

class Player(models.Model):
    pseudo = models.CharField(max_length=32, unique=True)
class PlayerResource(ModelResource):
    class Meta:
        queryset = Player.objects.all()
        resource_name = 'player'
        authentication = BasicAuthentication()
        authorization = Authorization()
        serializer = Serializer(formats=['xml'])
        filtering = {
            'pseudo': ALL,
        }
我请求所有玩家使用/api/v1/player/?format=xml,但似乎缺少响应标题:Content Length,这导致我的应用程序出现一些问题。如何将其添加到响应标题中


非常感谢。

您可以通过在自己的资源中覆盖create\u response方法来添加内容长度标题,例如:

class MyResource(ModelResource):
   class Meta:
        queryset=MyModel.objects.all()

   def create_response(self, ...)
      # Here goes regular code that do the method from tastypie
      return response_class(content=serialized, content_type=build_content_type(desired_format), Content-Length=value,  **response_kwargs)

您可以通过在自己的资源中覆盖create_Response方法来添加内容长度标题,例如:

class MyResource(ModelResource):
   class Meta:
        queryset=MyModel.objects.all()

   def create_response(self, ...)
      # Here goes regular code that do the method from tastypie
      return response_class(content=serialized, content_type=build_content_type(desired_format), Content-Length=value,  **response_kwargs)

缺少内容长度是由于缺少中间件。
有关更多信息: 看这里:

但是您可以像这样手动添加内容长度:

def create_response(self, request, data, response_class=HttpResponse, **response_kwargs):
        desired_format = self.determine_format(request)
        serialized = self.serialize(request, data, desired_format)
        response = response_class(content=serialized, content_type=build_content_type(desired_format), **response_kwargs)
        response['Content-Length'] = len(response.content)
        return response

缺少内容长度是由于缺少中间件。
有关更多信息: 看这里:

但是您可以像这样手动添加内容长度:

def create_response(self, request, data, response_class=HttpResponse, **response_kwargs):
        desired_format = self.determine_format(request)
        serialized = self.serialize(request, data, desired_format)
        response = response_class(content=serialized, content_type=build_content_type(desired_format), **response_kwargs)
        response['Content-Length'] = len(response.content)
        return response