草庐IT

python - AttributeError 'tuple' 对象没有属性 'get'

coder 2023-08-16 原文

我有一个 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 在我的代码中的位置?

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

最佳答案

你在这里返回一个元组:

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

关于python - AttributeError 'tuple' 对象没有属性 'get',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22557014/

有关python - AttributeError 'tuple' 对象没有属性 'get'的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  3. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  4. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  5. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  6. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  7. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  8. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  9. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  10. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

随机推荐