python - 属性错误 : 'int' object has no attribute 'get'

标签 python django django-views

我正在尝试在付款过程中请求结帐 ID,这是我目前的代码。 下面的代码工作正常,我从它那里得到了响应,但是一旦我将一些注释部分添加到数据字典中,它就会给我一个属性错误,说 int 没有对象获取。

基本上,我似乎只能将键和字符串或数字值添加到我的数据字典中,但每当我将字典添加为值时,它就会开始给我错误。

我不知道为什么会这样,有人可以帮忙吗?提前致谢!

def request_checkout_id(request):

    user = request.user

    url = "https://xxxxxx"
    entity_id = 'xxxxx'
    auth_header = 'xxxxx'
    currency = 'USD'

    try:
        ref_code = request.session['ref_code']
        order = Order.objects.get(ref_code=ref_code)    
    except:
        ref_code = None
        return redirect(reverse('shop:cart'))

    amount = float(order.final_total)

    base_con_tarifa = order.cart.total
    valor_iva = order.cart.tax_total
    #total_con_iva = order.sub_total
    base_sin_tarifa = order.shipping_price
    custom_param = '0081004012'+ str(valor_iva).replace(".", "").zfill(12) + '052012' + str(base_sin_tarifa).replace(".", "").zfill(12) + '003007010391005100817913101053012' + str(base_con_tarifa).replace(".", "").zfill(12)
    print(custom_param)

    data = {}
    # cart = {}
    # items = []
    # i = 0
    # for item in order.cart.cartitem_set.all():
    #     items.append({})
    #     items[i]['name'] = item.product.name
    #     items[i]['description'] = item.product.description
    #     items[i]['price'] = item.product.price
    #     items[i]['quantity'] = str(item.quantity)
    #     i += 1
    # cart['items'] = items
    #customer = {}
    #customer['givenName'] = user.first_name
    #customer['surname'] = user.last_name
    # customer['merchantCustomerId'] = str(user.id)
    # #customer['phone'] = order.shipping_address.phone
    # customer['phone'] = '0987543493'
    # customer['identificationDocType'] = 'IDCARD'
    # customer['identificationDocId'] = '0926685432'
    #shipping = {}
    # shipping['street1'] = order.shipping_address.street_address
    # shipping['country'] = order.shipping_address.country
    #shipping['street1'] = 'aosidaoisd'
    #shipping['country'] = 'EC'
    #billing = {}
    # billing['street1'] = order.billing_address.street_address
    # billing['country'] = order.billing_address.country
    #billing['street1'] = 'aosidaoisASd'
    #billing['country'] = 'EC'
    data['entityId'] = entity_id
    data['amount'] = amount
    data['currency'] = currency
    data['paymentType'] = 'DB'
    data['testMode'] = 'EXTERNAL'
    data['merchantTransactionId'] = 'as09aSAS097'
    #data['customer'] = customer
    #data['cart'] = cart
    #data['shipping'] = shipping
    #data['billing'] = billing
    print(data)

    try:
        opener = urllib.request.build_opener(urllib.request.HTTPHandler)
        request_id = urllib.request.Request(url, data=urllib.parse.urlencode(data).encode('utf-8'))
        request_id.add_header('Authorization', auth_header)
        request_id.get_method = lambda: 'POST'
        response = opener.open(request_id)
        parsed_response = json.loads(response.read())
        checkout_id = parsed_response['id']
        print(checkout_id)
    except urllib.error.HTTPError as e:
        print(e.code)
        return e.code

    if checkout_id:
        payment = Pago()
        payment.order = order
        payment.ref_code = order.ref_code
        payment.user = request.user
        payment.checkout_id = checkout_id
        payment.save()
        return redirect(reverse('shop:checkout'))
    #else:
    #raise some error or alert

这是我的错误回溯:

Internal Server Error: /shop/checkout/request_checkout_id
Traceback (most recent call last):
  File "C:\Users\Roberto\radlab\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\Roberto\radlab\lib\site-packages\django\utils\deprecation.py", line 96, in __call__
    response = self.process_response(request, response)
  File "C:\Users\Roberto\radlab\lib\site-packages\django\middleware\clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'int' object has no attribute 'get'
[31/Aug/2020 17:21:59] "GET /shop/checkout/request_checkout_id HTTP/1.1" 500 64423

最佳答案

对于以后阅读本文的任何人,关于此错误发生位置的更简单实例以及我的解决方案。

# Python program for reading from file 

counts = dict() *# line that fixed it for me*

fname = input("Enter file name: ")

with open(fname) as fh:
    myfile = fh.read()
    words = list(myfile.split())

    for word in words:
        counts[word] = counts.get(word,0) + 1

print('counts', counts)

事实证明,如果您没有将计数定义为 dict() 类型,它将抛出相同的 AttributeError: 'int' object has no attribute 'get' 错误。 我是 Python 的新手,所以如果这个解决方案太基础,它的目的是针对也在学习的人。

关于python - 属性错误 : 'int' object has no attribute 'get' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63675859/

相关文章:

python - 将 Python 列表转换为字典的简单方法

python - 有没有办法在谷歌应用引擎中查看内存缓存数据?

django - nginx "proxy_pass"指令中的参数数量无效

python - Django的 "Model"API线程安全吗?

python - django 模板中 "none"的等价物是什么?

Python PIL 如何将 1 位深度图像转换为 RGBA?

django - 如何限制 django admin 内联表单集

python - django if 语句中的括号

python - 从django中的用户输入读取csv文件

python - 强制脚本在 Python 3 中运行