【问题标题】:AttributeError 'tuple' object has no attribute 'get'AttributeError 'tuple' 对象没有属性 'get'
【发布时间】:2014-03-21 11:05:33
【问题描述】:

我有一个 Django 应用程序。但我不能犯一个我已经挣扎了一段时间的错误。

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

这是 django 为我提供的回溯。

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:

我很难弄清楚为什么会出现这个错误。我怎样才能找到tuple 在我的代码中的位置?

观点:

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

【问题讨论】:

  • 您可以将这两行if "pris" in request.GET: price_filtering = request.GET.get("pris", None) 替换为price_filtering = request.GET.get('pris', None),您的第一次成员资格检查不是必需的,因为get 永远不会引发异常。

标签: python django


【解决方案1】:

你在这里返回一个元组:

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

您是否忘记在此处添加render

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

【讨论】:

    【解决方案2】:

    始终确保在“返回”之后在视图函数中具有 render

    就我而言,我没有添加return render(request, app_name/index.html 我花了很多时间才找到这个错误,甚至没有一个关于堆栈溢出的答案提到它,这就是我在这里发布它的原因。

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

    【讨论】:

      【解决方案3】:

      views.py

      return函数的末尾多了一个逗号“,”
      return render(request, 'index.html',{}), #incorrect
      

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

      【讨论】:

        【解决方案4】:

        即使在视图中使用逗号也很简单

        原样 这个

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

        而不是 这个

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

        【讨论】:

          【解决方案5】:

          您忘记在return 之后添加render 所以你只需要在return之后添加render

          你写的部分(看最后一个 elif):

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

          但它会是:

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

          【讨论】:

            猜你喜欢
            • 2020-11-07
            • 2019-05-23
            • 2020-04-21
            • 1970-01-01
            • 2021-03-18
            • 2023-01-26
            • 1970-01-01
            • 2015-06-22
            • 1970-01-01
            相关资源
            最近更新 更多