Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError 'tuple' object has no attribute 'get'

Tags:

python

django

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"]
        })
like image 951
nkobber Avatar asked Mar 21 '14 11:03

nkobber


People also ask

How do you fix tuple object has no attribute?

The Python "AttributeError: 'tuple' object has no attribute" occurs when we access an attribute that doesn't exist on a tuple. To solve the error, use a list instead of a tuple to access a list method or correct the assignment.

What does object has no attribute mean in Python?

It's simply because there is no attribute with the name you called, for that Object. This means that you got the error when the "module" does not contain the method you are calling.

What is tuple error in Python?

The “TypeError: 'tuple' object is not callable” error is raised when you try to call a tuple as a function. This can happen if you use the wrong syntax to access an item from a tuple or if you forget to separate two tuples with a comma. Make sure when you access items from a tuple you use square brackets.

What is attribute error in Python?

AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. For example, if we take a variable x we are assigned a value of 10. In this process suppose we want to append another value to that variable. It's not possible.

Why do I get an attributeerror when getting a tuple?

If we call the get () method on the tuple value, Python will raise an AttributeError: ‘tuple’ object has no attribute ‘get’. The error can also happen if you have a method which returns an tuple instead of a dictionary. Let us take a simple example to reproduce this error.

What is a tuple in Python?

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. I am also new to Python so I might be on the wrong track but maybe vf1 should not be of type tuple.

Why do I get dot-access error when returning a tuple?

This error occurs when attempting to access the values of a tuple incorrectly. Functions that return multiple variables will output the results in a tuple, so we cannot use dot-access to retrieve the values. For example, let's create a simple function that returns two values:

Do tuple values need to be unpacked before accessing?

The solution shows that tuple values need to be unpacked and assigned to variables before directly accessing them. Note that we can unpack our function output straight into add, mult, and div, without having to use results as a go-between: This article contains a few more examples of unpacking functions and lists.


2 Answers

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,
    })
like image 118
Martijn Pieters Avatar answered Sep 30 '22 03:09

Martijn Pieters


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
like image 23
Zahid Khan Avatar answered Sep 30 '22 03:09

Zahid Khan