23

I have a Django application. But i can't an error that i have been struggling with for some time now.

Exception Value: 'tuple' object has no attribute 'get'

Exception Location: /Library/Python/2.7/site-packages/django/middleware/clickjacking.py in process_response, line 30

And this is the traceback django provides me.

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
201.                 response = middleware_method(request, response)
File "/Library/Python/2.7/site-packages/django/middleware/clickjacking.py" in process_response
30.         if response.get('X-Frame-Options', None) is not None:

I have a hard time figuring out why this error occurs. How can i find out where that tuple is in my code?

The view:

def products(request, category=None, gender=None, retailer_pk=None, brand_pk=None):
    # If the request it doesn't have a referer use the full path of the url instead.
    if not request.META.get("HTTP_REFERER", None):
        referer = request.get_full_path()
    else:
        referer = request.META.get("HTTP_REFERER")

    if gender:
        products = ProductSlave.objects.filter(Q(productmatch__stockitem__stock__gt=0) &
                                               Q(productmatch__slave_product__master_product__gender=gender)).distinct()
    elif brand_pk:
        products = ProductSlave.objects.filter(Q(master_product__brand__pk=brand_pk))
        brand = Brand.objects.get(pk=brand_pk)
    elif retailer_pk:
        retailer = Retailer.objects.get(pk=retailer_pk)
        products = retailer.productmatch_set.all()
    else:
        products = ProductSlave.objects.filter(Q(productmatch__stockitem__stock__gt=0))

    if not retailer_pk:
        filt = create_filtering(productslaves=products, request=request)
    elif retailer_pk:
        filt = create_filtering(productmatches=products, request=request)

    sorting_selected = request.GET.get("sorter", None)

    # q_list is used to store all the Q's
    # Then they are reduced. And then filtered
    if "brand" in request.GET:
        brand_filtering = request.GET.get("brand", None)
        if brand_filtering:
            brand_filtering = brand_filtering.split("|")
        q_list = []
        for filter in brand_filtering:
            if retailer_pk:
                q_list.append(Q(slave_product__master_product__brand__slug=filter))
            else:
                q_list.append(Q(master_product__brand__slug=filter))
        reduced_q = reduce(operator.or_, q_list)
        products = products.filter(reduced_q)

    if "kategori" in request.GET:
        category_filtering = request.GET.get("kategori", None)
        if category_filtering:
            category_filtering = category_filtering.split("|")
        q_list = []
        for filter in category_filtering:
            if retailer_pk:
                q_list.append(Q(slave_product__master_product__product_category__slug=filter))
            else:
                q_list.append(Q(master_product__product_category__slug=filter))
        reduced_q = reduce(operator.or_, q_list)
        products = products.filter(reduced_q)

    if "stoerrelse" in request.GET:
        size_filtering = request.GET.get("stoerrelse", None)
        if size_filtering:
            size_filtering = size_filtering.split("|")
        q_list = []
        for filter in size_filtering:
            if retailer_pk:
                q_list.append(Q(slave_product__productmatch__stockitem__size__pk=filter))
            else:
                q_list.append(Q(productmatch__stockitem__size__pk=filter))
        reduced_q = reduce(operator.or_, q_list)
        products = products.filter(reduced_q)

    if "farve" in request.GET:
        color_filtering = request.GET.get("farve", None)
        if color_filtering:
            color_filtering = color_filtering.split("|")
        q_list = []
        for filter in color_filtering:
            if retailer_pk:
                q_list.append(Q(slave_product__sorting_color__slug=filter))
            else:
                q_list.append(Q(sorting_color__slug=filter))
        reduced_q = reduce(operator.or_, q_list)
        products = products.filter(reduced_q)

    if "pris" in request.GET:
        price_filtering = request.GET.get("pris", None)
        if price_filtering:
            price_filtering = price_filtering.split("|")
        q_list = []
        if retailer_pk:
            q_list.append(Q(price__gte=price_filtering[0]))
            q_list.append(Q(price__lte=price_filtering[1]))
        else:
            q_list.append(Q(productmatch__price__gte=price_filtering[0]))
            q_list.append(Q(productmatch__price__lte=price_filtering[1]))
        reduced_q = reduce(operator.and_, q_list)
        products = products.filter(reduced_q)

    if sorting_selected:
        if sorting_selected == filt["sorting"][0]["name"]:
            filt["sorting"][0]["active"] = True
            if retailer_pk:
                products = products.annotate().order_by("-price")
            else:
                products = products.annotate().order_by("-productmatch__price")
        elif sorting_selected == filt["sorting"][1]["name"]:
            if retailer_pk:
                products = products.annotate().order_by("price")
            else:
                products = products.annotate().order_by("productmatch__price")
            filt["sorting"][1]["active"] = True
        elif sorting_selected == filt["sorting"][2]["name"]:
            filt["sorting"][2]["active"] = True
            if retailer_pk:
                products = products.order_by("pk")
            else:
                products = products.order_by("pk")
        else:
            if retailer_pk:
                products = products.order_by("pk")
            else:
                products = products.order_by("pk")
    else:
        if retailer_pk:
            products = products.order_by("pk")
        else:
            products = products.order_by("pk")

    if brand_pk:
        return render(request, 'page-brand-single.html', {
            'products': products,
            "sorting": filt["sorting"],
            "filtering": filt["filtering"],
            "brand": brand,
        })
    elif retailer_pk:
        return (request, 'page-retailer-single.html', {
            "products": products,
            "sorting": filt["sorting"],
            "filtering": filt["filtering"],
            "retailer": retailer,
        })
    else:
        return render(request, 'page-product-list.html', {
            'products': products,
            "sorting": filt["sorting"],
            "filtering": filt["filtering"]
        })
1
  • 1
    You can replace these two lines if "pris" in request.GET: price_filtering = request.GET.get("pris", None) with price_filtering = request.GET.get('pris', None), your first membership check is not required because get will never raise an exception. Commented Mar 21, 2014 at 11:12

9 Answers 9

77

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,
    })
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

2

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

Comments

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,
    })

Comments

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

Comments

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

1 Comment

Please don't repeat answers
0

Had the same error, spent some time scratching my head before realizing that in one of my forms, i had:

def __init__(self, *args, **kwargs) -> None:
        super(MyForm, self).__init__(args, kwargs)

Instead of: (notice the asterisks before args & kwargs)

def __init__(self, *args, **kwargs) -> None:
        super(MyForm, self).__init__(*args, **kwargs)

Comments

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.