0

I'm a beginner in python. I'm not able to understand what the problem is?

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'
1
  • 1
    Please show your code for the view. Commented Dec 25, 2019 at 4:23

2 Answers 2

0

Whatever middleware_method returns is a tuple, so in the form ('a', 1, []) or something.

The error is telling you that you can't access members of the tuple by name, because they don't have names.

Maybe you created a tuple like this:

status_code = 404
name = 'Not found'
response = (name, status_code)

Once you declare the tuple, the names that went into it are lost. You have a couple of options for getting things out.

Direct access

You can get the objects by index, like you would with a list:

assert response[1] == 404

If you don't know what the tuple looks like, just print it, and calculate the index.

Named tuple

If you're determined to use names, you can create a namedtuple, provided that the tuple is going to be in the same format every time.

from collections import namedtuple

Response = namedtuple('Response', ('name', 'status_code')
response = Response('Not found', 404)

assert response.status_code == 404

Alternatively there may be a mistake in your code, where you are inadvertently returning a tuple, but one part of it is a requests.Response object. In that case, you can just extract the object as in "Direct access", and then use as you are already.

Would have to see the code to be of more help, but it might be something like:

response[2].status_code
Sign up to request clarification or add additional context in comments.

Comments

0

I will try to explain how this error comes with a simple example

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()

This will return the error

AttributeError: 'tuple' object has no attribute 'a1'

Because I have returned three variables a1, b1, c1 in example_error function and trying to get them by using single variable strings.

I can get rid of this by using the following modified call_function

def call_function():
    strings = example_error()
    s1 = strings[0]
    s2 = strings[1]
    s3 = strings[2]
    print(s1, s2, s3)
call_function()

As you did not show your code, I assume you have done something similar as here in the first case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.