0

我正在使用Django Rest Framework来构建一个 api,并在我的开发机器上实现了。我今天想快速部署它,事实证明它比我预期的更容易出错。我重新安装了依赖项,重新安装了 Django,花了几个小时寻找错误,但是这个最新的已经打败了我和谷歌。

在开发机器上,当我从框架调用其中一个视图时,我得到了正确的响应(减去静态文件,但我稍后会处理。)然而,在部署机器上,我得到了这个错误:

'SplitResult' object has no attribute '_replace'

回溯通过rest框架中的一个模板标签,但是这个方法中的urlobject中似乎抛出了错误:

def __replace(self, **replace):
        """Replace a field in the ``urlparse.SplitResult`` for this URL."""
        return type(self)(urlparse.urlunsplit(
            urlparse.urlsplit(self)._replace(**replace))) 

我无法在我的开发机器上重现此错误,并且在任何地方都找不到对此错误的任何引用。以防万一我在部署服务器上使用 Django 1.2、1.3 和 1.4 进行了尝试,但该错误似乎与 Django 本身无关。

这是完整的回溯:

Django Version: 1.3.1
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'char',
 'djangorestframework']
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')


Traceback:
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/views/generic/base.py" in view
  47.             return self.dispatch(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
  39.         resp = view_func(*args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
  52.         return view_func(*args, **kwargs)
File "/home/wfrp/wfrp-django/DjangoWfrp/external apps/django-rest-framework/djangorestframework/views.py" in dispatch
  243.         return self.final(request, response, *args, **kwargs)
File "/home/wfrp/wfrp-django/DjangoWfrp/external apps/django-rest-framework/djangorestframework/views.py" in final
  196.         return self.render(response)
File "/home/wfrp/wfrp-django/DjangoWfrp/external apps/django-rest-framework/djangorestframework/mixins.py" in render
  251.             content = renderer.render(response.cleaned_content, media_type)
File "/home/wfrp/wfrp-django/DjangoWfrp/external apps/django-rest-framework/djangorestframework/renderers.py" in render
  351.         ret = template.render(context)
File "/usr/lib/python2.5/site-packages/django/template/base.py" in render
  123.             return self._render(context)
File "/usr/lib/python2.5/site-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/usr/lib/python2.5/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/usr/lib/python2.5/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/usr/lib/python2.5/site-packages/django/template/loader_tags.py" in render
  127.         return compiled_parent._render(context)
File "/usr/lib/python2.5/site-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/usr/lib/python2.5/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/usr/lib/python2.5/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/usr/lib/python2.5/site-packages/django/template/defaulttags.py" in render
  311.             return self.nodelist_true.render(context)
File "/usr/lib/python2.5/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/usr/lib/python2.5/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/usr/lib/python2.5/site-packages/django/template/defaulttags.py" in render
  227.                 nodelist.append(node.render(context))
File "/usr/lib/python2.5/site-packages/django/template/defaulttags.py" in render
  500.         output = self.nodelist.render(context)
File "/usr/lib/python2.5/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/usr/lib/python2.5/site-packages/django/template/base.py" in render_node
  757.         return node.render(context)
File "/usr/lib/python2.5/site-packages/django/template/base.py" in render
  792.             output = self.filter_expression.resolve(context)
File "/usr/lib/python2.5/site-packages/django/template/base.py" in resolve
  536.                 new_obj = func(obj, *arg_vals)
File "/home/wfrp/wfrp-django/DjangoWfrp/external apps/django-rest-framework/djangorestframework/templatetags/add_query_param.py" in add_query_param
  7.     return unicode(URLObject(url).with_query(param))
File "/home/wfrp/wfrp-django/DjangoWfrp/external apps/urlobject/lib/urlobject/urlobject.py" in with_query
  117.         return self.__replace(query=query)
File "/home/wfrp/wfrp-django/DjangoWfrp/external apps/urlobject/lib/urlobject/urlobject.py" in __replace
  181.             urlparse.urlsplit(self)._replace(**replace)))

Exception Type: AttributeError at /api/
Exception Value: 'SplitResult' object has no attribute '_replace'

这是我第一次使用 Stack Overflow — 感谢您的帮助。

4

1 回答 1

1

我怀疑您的开发和部署机器正在运行不同的 Python 版本。SplitResult的Python 文档没有提及有关内部_replace方法的任何内容,但前面的下划线表明它是一个内部 API。这是一个 Django Rest Framework 错误,我建议向他们提交错误报告。

如果你好奇,SplitResult它是namedtuple(在 Python 2.6 中引入)的子类。它需要 kwargs 并返回一个SplitResult替换相关值的新值。

In [16]: sr = urlparse.urlsplit("http:///www.google.com")

In [17]: sr
Out[17]: SplitResult(scheme='http', netloc='', path='/www.google.com', query='', fragment='')

In [18]: sr._replace(scheme='https')
Out[18]: SplitResult(scheme='https', netloc='', path='/www.google.com', query='', fragment='')

我相信SplitResultPython < 2.6 中的版本是 的子类tuple,它没有_replace方法,可以解释您看到的错误

于 2012-04-10T18:27:33.210 回答