【发布时间】:2022-06-10 21:16:09
【问题描述】:
我有一个 DJANGO 字典,我试图通过 API 将它发送到另一个服务,但是在指定密钥时,我在 DJANGO 中收到错误。 TypeError: 列表索引必须是整数或切片,而不是 str。
这是我的字典的输出。
"receipt_details": [
{
"product__name": [
{
"product__name": "Pokemon 25th Anniversary",
"quanity": 1,
"product__point_price": 100.0,
"order_id": 32
},
{
"product__name": "Minecraft - Nintendo Switch",
"quanity": 1,
"product__point_price": 100.0,
"order_id": 32
}
],
这是我如何创建字典的代码。
items = order.orderitem_set.all()
data = items.values("product__name","quanity","product__point_price", "order_id")
data_list = list(data)
receipt_details = {
}
receipt_details['receipt_details'] = {}
for count, data in enumerate (data_list):
receipt_details = data_list
我知道它正在寻找我指定索引,但是我有多个要发送的项目。那么我该怎么做才能克服呢?谢谢
这是我要发送的 API 信息。
#POST MARK API
url = "https://api.postmarkapp.com/email/withTemplate"
payload = json.dumps({
"From": "",
"To": "",
"TemplateId": 28132290,
"TemplateModel": {
"counselor_name": "Fellow Counselor",
"student_name": student.student_name,
"receipt_id": str(order.transaction_id),
"date": today_date_strf,
"points": order.get_cart_total,
"receipt_details": [
{
"product__name": receipt_details['product__name']['product__name'], <-- Wants me to set Index here
"amount": "amount_Value"
}
]
},
"MessageStream": "outbound"
})
headers = {
'Content-Type': 'application/json',
'X-Postmark-Server-Token': settings.POSTMARK["TOKEN"]
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
【问题讨论】:
-
哪一行报错了?
-
以上代码中的
order和orderitem_set是什么? -
也很有用:提供MRE的提示