我正在尝试在付款过程中要求提供结帐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
答案 0 :(得分:1)
对于将来阅读本文的任何人,请提供有关发生此错误的更简单实例以及我的解决方案。
# 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 很陌生,所以如果这个解决方案太基础,它的目的是针对也在学习的人。
答案 1 :(得分:0)
正如注释中指出的那样,问题是在int
情况下,您以HTTPError.code
(HTTPError
)的形式返回状态代码,而Django需要HTTPResponse
。
处理此问题的最简单方法是替换此逻辑:
except urllib.error.HTTPError as e:
print(e.code)
return e.code
与此:
except urllib.error.HTTPError as e:
print(e.code)
return HttpResponse(status=e.code)
但是,根据您的应用程序,您可能需要更多的HTTPResponse。您可以在returning errors的Django文档中阅读更多内容。对于特定的状态代码,还有HTTPResponse
的子类,因此,如果您知道错误代码为500,则可以返回HttpResponseServerError
。
答案 2 :(得分:0)
已经解决了。我只是以错误的格式传递数据。