【问题标题】:'HttpResponse' has no attribute '_get_content'HttpResponse' 没有属性 '_get_content
【发布时间】:2013-04-24 01:20:50
【问题描述】:

我正在使用 Django 1.5.1 和 Piston 来支持 MongoDB 数据库。

在尝试测试其余 url 以检索数据时,我收到以下错误。

type object 'HttpResponse' has no attribute '_get_content'
Request Method: GET
Request URL:    http://127.0.0.1:8000/annotation/search?limit=20&uri=/document
Django Version: 1.5.1
Exception Type: AttributeError
Exception Value:    
type object 'HttpResponse' has no attribute '_get_content'
Exception Location: /home/tank/sites/python/env.example/lib/python2.7/site-packages/piston/utils.py in HttpResponseWrapper, line 72
Python Executable:  /home/tank/sites/python/env.example/bin/python
Python Version: 2.7.4

问题是在活塞代码中产生的,不知道是什么原因。这是完整的回溯

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/annotations/search?limit=20&uri=/templates

Django Version: 1.5.1
Python Version: 2.7.4
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.flatpages',
 'app.modules.members',
 'app.modules.cms',
 'app.modules.annotator',
 'debug_toolbar')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "/home/tank/sites/python/env.example/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/tank/sites/python/env.example/lib/python2.7/site-packages/django/views/decorators/vary.py" in inner_func
  19.             response = func(*args, **kwargs)
File "/home/tank/sites/python/env.example/lib/python2.7/site-packages/piston/resource.py" in __call__
  166.             result = self.error_handler(e, request, meth, em_format)
File "/home/tank/sites/python/env.example/lib/python2.7/site-packages/piston/resource.py" in error_handler
  257.             result = rc.BAD_REQUEST
File "/home/tank/sites/python/env.example/lib/python2.7/site-packages/piston/utils.py" in __getattr__
  51.         class HttpResponseWrapper(HttpResponse):
File "/home/tank/sites/python/env.example/lib/python2.7/site-packages/piston/utils.py" in HttpResponseWrapper
  72.             content = property(HttpResponse._get_content, _set_content)            

Exception Type: AttributeError at /annotations/search
Exception Value: type object 'HttpResponse' has no attribute '_get_content'

/谢谢

“HttpResponseWrapper”对象的新错误引用没有属性“_is_string”:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/annotations/search?limit=20&uri=

Django Version: 1.5.1
Python Version: 2.7.4
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.flatpages',
 'app.modules.members',
 'app.modules.cms',
 'app.modules.annotator',
 'debug_toolbar')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "/home/tank/sites/python/env.example/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/tank/sites/python/env.example/lib/python2.7/site-packages/django/views/decorators/vary.py" in inner_func
  19.             response = func(*args, **kwargs)
File "/home/tank/sites/python/env.example/lib/python2.7/site-packages/piston/resource.py" in __call__
  184.         if isinstance(result, HttpResponse) and not result._is_string:

Exception Type: AttributeError at /annotations/search
Exception Value: 'HttpResponseWrapper' object has no attribute '_is_string'

【问题讨论】:

    标签: django django-piston


    【解决方案1】:

    在 Django 1.5 中,此代码:

    def _get_content(self):
        # do some stuff
    
    def _set_content(self):
        # do some stuff
    
    content = property(_get_content, _set_content)
    

    改为:

    @property
    def content(self):
        # do some stuff
    
    @content.setter
    def content(self, value):
        # do some stuff
    

    所以,Django 1.5 没有_get_content 功能。您应该将 piston/utils.py 中的 HttpResponseWrapper 更改为如下内容:

    class HttpResponseWrapper(HttpResponse):
        """
        Wrap HttpResponse and make sure that the internal
        _is_string/_base_content_is_iter flag is updated when the
        _set_content method (via the content property) is called
        """
    
        def _set_content(self, content):
            """
            Set the _container and _is_string /
            _base_content_is_iter properties based on the type of
            the value parameter. This logic is in the construtor
            for HttpResponse, but doesn't get repeated when
            setting HttpResponse.content although this bug report
            (feature request) suggests that it should:
            http://code.djangoproject.com/ticket/9403
            """
            is_string = False
            if not isinstance(content, basestring) and hasattr(content, '__iter__'):
                self._container = content
            else:
                self._container = [content]
                is_string = True
            if django.VERSION >= (1, 4):
                self._base_content_is_iter = not is_string
            else:
                self._is_string = is_string
    
        try:
            # Django versoin is older than 1.5
    
            content = property(HttpResponse._get_content, _set_content)
    
        except:
            # Django version 1.5
    
            @HttpResponse.content.setter
            def content(self, content):
                self._set_content(content)
    

    让我知道它是否有效,我会将其提交给piston 代码。


    关于第二个问题:也许你的版本太旧了。这个问题已经在thiscommit 中解决了。

    【讨论】:

    • 我收到以下错误:'HttpResponseWrapper' object has no attribute '_is_string' Request Method: GET Request URL: http://127.0.0.1:8000/annotations/search?limit=20&uri= Django Version: 1.5.1 Exception Type: AttributeError Exception Value: 'HttpResponseWrapper' object has no attribute '_is_string' Exception Location: /home/tank/sites/python/env.example/lib/python2.7/site-packages/piston/resource.py in __call__, line 184
    • @tank,您能否将新的完整回溯添加到问题中?
    • 我正在使用 pip 包管理器来安装活塞。我拥有的版本是 django-piston (0.2.3),似乎是最新的。也许变化只在主人身上。无论如何,上面的代码有效。
    【解决方案2】:

    您可以通过更新到此活塞前叉的最新版本来解决此问题: https://github.com/marcio0/django-piston/

    【讨论】:

      猜你喜欢
      • 2017-06-14
      • 1970-01-01
      • 2016-09-19
      • 2016-09-17
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多