Python 属性错误:';元组';对象没有属性';状态代码';
我是python的初学者。我不明白问题出在哪里Python 属性错误:';元组';对象没有属性';状态代码';,python,django,python-2.7,Python,Django,Python 2.7,我是python的初学者。我不明白问题出在哪里 the runtime process for the instance running on port 43421 has unexpectedly quit ERROR 2019-12-24 17:29:10,258 base.py:209] Internal Server Error: /input/ Traceback (most recent call last): File "/var/www/html/sym_math/g
the runtime process for the instance running on port 43421 has unexpectedly quit
ERROR 2019-12-24 17:29:10,258 base.py:209] Internal Server Error: /input/
Traceback (most recent call last):
File "/var/www/html/sym_math/google_appengine/lib/django-1.3/django/core/handlers/base.py", line 178, in get_response
response = middleware_method(request, response)
File "/var/www/html/sym_math/google_appengine/lib/django-1.3/django/middleware/common.py", line 94, in process_response
if response.status_code == 404:
AttributeError: 'tuple' object has no attribute 'status_code'
任何
中间件\u方法
返回的都是元组
,因此以('a',1,[])
或其他形式
错误告诉您不能按名称访问元组成员,因为它们没有名称
也许您创建了这样一个元组:
status_code = 404
name = 'Not found'
response = (name, status_code)
声明元组后,进入元组的名称将丢失。你有两个选择来解决问题
直接访问
您可以按索引获取对象,就像使用列表一样:
assert response[1] == 404
如果您不知道元组是什么样子,只需打印它,然后计算索引
命名元组
如果决定使用名称,则可以创建一个namedtuple
,前提是该tuple每次都采用相同的格式
from collections import namedtuple
Response = namedtuple('Response', ('name', 'status_code')
response = Response('Not found', 404)
assert response.status_code == 404
或者,代码中可能存在错误,您无意中返回了一个元组,但其中一部分是
requests.Response
对象。在这种情况下,您可以只提取“直接访问”中的对象,然后按原样使用
必须查看代码才能提供更多帮助,但可能是:
response[2].status_code
我将用一个简单的例子来解释这个错误是如何产生的
def example_error():
a1 = "I am here"
b1 = "you are there"
c1 = "This is error"
return a1, b1, c1
def call_function():
strings = example_error()
s1 = strings.a1
s2 = strings.b1
s3 = strings.c1
print(s1, s2, s3)
call_function()
这将返回错误
AttributeError: 'tuple' object has no attribute 'a1'
因为我在示例_error函数中返回了三个变量a1、b1、c1,并尝试使用单变量字符串来获取它们
我可以通过使用下面修改的call_函数来消除这个问题
def call_function():
strings = example_error()
s1 = strings[0]
s2 = strings[1]
s3 = strings[2]
print(s1, s2, s3)
call_function()
由于您没有显示代码,我假设您做了与第一种情况类似的操作。请为视图显示代码。