AttributeError'tuple'对象没有属性'get' [英] AttributeError 'tuple' object has no attribute 'get'

查看:2411
本文介绍了AttributeError'tuple'对象没有属性'get'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django应用程序。但是我现在不能一直在努力的错误。

 异常值:'tuple'对象有没有属性'get'

异常位置:/Library/Python/2.7/site-packages/django/middleware/clickjacking.py in process_response,第30行

这是追溯django提供给我。

 追溯:
文件/Library/Python/2.7/site-packages/django/core/handlers/base.pyin get_response
201. response = middleware_method(request,response)
文件/Library/Python/2.7/site-packages/django/middleware/clickjacking.pyin process_response
30.如果response.get('X-Frame-Options',None)不是None:

我很难弄清楚为什么会出现此错误。我如何找到我的代码中的 tuple 在哪里?



视图:

  def products(request,category = None,gender = None,retailer_pk = None,brand_pk = None):
#如果请求不要让引用者使用URL的完整路径。
如果没有request.META.get(HTTP_REFERER,无):
referer = request.get_full_path()
else:
referer = request.META.get(HTTP_REFERER )

如果性别:
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) )

如果不是retailer_pk:
filt = create_filtering(productslaves = products,request = request)
elif retailer_pk:
filt = create_filtering(productmatches = products,request = request)

sorted_selected = request.GET.get(sorter,None)

#q_list用于存储所有Q
#然后他们被减少。然后过滤
如果请求中的品牌.GET:
brand_filtering = request.GET.get(brand,None)
如果brand_filtering:
brand_filtering = brand_filtering.split (|)
q_list = []
在brand_filtering中过滤:
如果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在request.GET:
category_filtering = request.GET.get(kategori,无)
如果category_filtering:
category_filtering = category_filtering.split(|)
q_list = []
在category_filtering中过滤:
如果retailer_pk:
q_list.append(Q(slave_product__master_product__produc

$ b q_list.append(Q(master_product__product_category__slug = filter))
reduced_q = reduce(operator.or_,q_list)
products = products.filter(

如果stoerrelse在request.GET:
size_filtering = request.GET.get(stoerrelse,无)
如果size_filtering:
size_filtering = size_filtering.split(|)
q_list = []
在size_filtering中过滤:
如果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)

如果farve在request.GET:
color_filtering = request.GET.get(farve,无)
如果color_filtering:
color_filtering = color_filtering.split(|)
q_list = []
color_filtering中的过滤器:
如果retailer_pk:
q_list.append(Q(slave_product__sorting_color__slug = filter))
else:
q_list.append(Q(sorted_color__slug = filter))
reduced_q = reduce(operator.or_,q_list)
products = products.filter(reduced_q)

如果pris在request.GET:
price_filtering = request.GET.get(pris,无)
如果price_filtering:
price_filtering = price_filtering.split(| )
q_list = []
如果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]))
reduc ed_q = reduce(operator.and_,q_list)
products = products.filter(reduced_q)

如果sorted_selected:
如果sort_selected == filt [sorted] [0] [name]:
filt [sort] [0] [active] = True
如果retailer_pk:
products = products.annotate()。order_by )
else:
products = products.annotate()。order_by( - productmatch__price)
elif sorted_selected == filt [sorting] [1] [name]:
如果retailer_pk:
products = products.annotate()。order_by(price)
else:
products = products.annotate()。order_by(productmatch__price)
filt [sort] [1] [active] = True
elif sorted_selected == filt [sort] [2] [name]:
filt [ ] [2] [active] = True
如果retailer_pk:
products = products.order_by(pk)
否则:
products = products.order_by(pk)
else:
如果retailer_pk:
products = products.order_by(pk)
else:
products = products.order_by(pk)
else:
如果retailer_pk:
products = products.order_by(pk)
else:
product = products.order_by(pk)

如果brand_pk:
返回呈现(请求,page-brand-single.html,{
'products':products ,
排序:filt [sorting],
过滤:filt [filters],
brand:brand,
})
elif retailer_pk:
return(request,'page-retailer-single.html',{
products:products,
sorting:filt [sorting],
过滤:filt [过滤],
零售商:零售商,
})
其他:
return render(request,'page-product-list.html',{
'products':products,
sorting:filt [sorting],
过滤:filt [filtering]
})


解决方案>

您正在返回一个元组:

  elif retailer_pk:
return(request,'page-retailer -single.html',{
products:products,
sorting:filt [sorting],
filters:filt [filter],
零售商:零售商,
})

你忘了添加 render 可能:

  elif retailer_pk:
return render(request, 'page-retailer-single.html',{
products:products,
sorting:filt [sorting],
filters ,
零售商:零售商,
})


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

解决方案

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

这篇关于AttributeError'tuple'对象没有属性'get'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆