[Django]-AttributeError 'tuple' object has no attribute 'get'

72👍

You are returning a tuple here:

elif retailer_pk:
    return (request, 'page-retailer-single.html', {
        "products": products,
        "sorting": filt["sorting"],
        "filtering": filt["filtering"],
        "retailer": retailer,
    })

Did you forget to add render there perhaps:

elif retailer_pk:
    return render(request, 'page-retailer-single.html', {
        "products": products,
        "sorting": filt["sorting"],
        "filtering": filt["filtering"],
        "retailer": retailer,
    })

7👍

There is an extra comma "," at the end of return function in views.py

return render(request, 'index.html',{}), #incorrect

return render(request, 'index.html',{}) #correct

7👍

Always make sure you have render after ‘return’, in your views functions

In my case I had not added the return render(request, app_name/index.html
It took me a lot of time to find this bug and not even one of the answers on stack overflow mentioned it, that’s why I posted it here.

{IMAGE} check the index and register functions and the error indicated below them.

1👍

Even taking the commas of the views is as simple of a solution

as-in
this

def home(request):
    return render(request, 'index.html')

def hire(request):
    return render(request, 'hire.html')

def handler400(request, exception):
    return render(request, '400.html', locals())

def handler403(request, exception):
    return render(request, '403.html', locals())

def handler404(request, exception):
    return render(request, '404.html', locals())

def handler500(request, exception):
    return render(request, '500.html', locals())

Rather than
this

def home(request):
    return render(request, 'index.html'),

def hire(request):
    return render(request, 'hire.html'),

def handler400(request, exception):
    return render(request, '400.html', locals()),

def handler403(request, exception):
    return render(request, '403.html', locals()),

def handler404(request, exception):
    return render(request, '404.html', locals()),

def handler500(request, exception):
    return render(request, '500.html', locals())
👤Phil

1👍

you forgot to add render after return
so you just need to add render after return

The part you wrote(look at the last elif):

elif retailer_pk:
    return (request, 'page-retailer-single.html', {
        "products": products,
        "sorting": filt["sorting"],
        "filtering": filt["filtering"],
        "retailer": retailer,
    })

but it would be :

elif retailer_pk:
    return render(request, 'page-retailer-single.html', {
        "products": products,
        "sorting": filt["sorting"],
        "filtering": filt["filtering"],
        "retailer": retailer,
    })

0👍

Had a similar problem and discovered that the error was from the spacing between the function name and the request

I did this : 
def greet (request, name):
    return render(request, "hello/greet.html", {
        "name": name. Capitalize()
    })

instead of this:
def greet(request, name):
    return render(request, "hello/greet.html", {
        "name": name. Capitalize()
    })

The space between 'greet' and '(request, name)' was responsible for the error

0👍

simply the problem is, there is no extra comma after the bracket

before

def home(request):
return render(request,'home.html',{}), //error bcz of  extra comma

after

def home(request):

return render(request,'home.html',{}) //no  error

-2👍

This bug took me so much time to find, and I couldn’t find the answer here. Just something similar, so I decided to drop this exact one here:

Incorrect:

return render(request, 'templates folder directory', {'team': team}, {'form': form}) 

Correct:

return render(request, 'templates folder directory', {'team': team, 'form': form }) 

Leave a comment