我想利用Townscript提供的webhook更新模型中的is_paid字段。
数据格式为(json dict):-link(在服务器通知API列下)。
链接上的一条有用信息是:
我们将使用后置参数 data 以以下格式发送数据。
这是python代码:
def payment(request):
if request.method == "POST":
posts=request.POST["data"]
result=json.dumps(posts) #converting incoming json dict to string
paymentemail(result) # emailing myself the string (working fine)
data = posts
try:
user = Profile.objects.filter(email=data['userEmailId']).first()
user.is_paid = True
user.save()
except Exception as e: paymentemail(str(e)) # emailing myself the exception
return redirect('/home')
else:
.......
上面代码中与 paymentemail()功能相对应的两封电子邮件是:
"{\"customQuestion1\":\"+9175720*****\",\"customAnswer205634\":\"+917572******\",
\"customQuestion2\":\"**** University\",\"customQuestion205636\":\"College Name\",\"ticketPrice\":**00.00,
\"discountAmount\":0.00,\"uniqueOrderId\":\"***********\",\"userName\":\"********\",
\"customQuestion205634\":\"Contact Number\",\"eventCode\":\"**********\",\"registrationTimestamp\":\"12-12-2019 22:22\",
\"userEmailId\":\"***********@gmail.com\",etc....}"
我知道反斜杠用于转义引号。
第二封电子邮件:(例外)
string indices must be integers
这是否意味着data = request.POST ['data']给我一个字符串,从而在我使用data ['userEmailId']时导致错误?我该如何处理这个错误?
答案 0 :(得分:0)
def payment(request):
if request.method == "POST":
posts=request.POST
result=json.dumps(posts) #converting incoming json dict to string
paymentemail(result) # emailing myself the string (working fine)
try:
user = Profile.objects.filter(email=posts['userEmailId']).first()
user.is_paid = True
user.save()
except Exception as e: paymentemail(str(e)) # emailing myself the exception
return redirect('/home')
else:
.......
我更改了posts=request.POST["data"]
,因为它将仅获取键“数据”的值,而我使用posts=request.POST
,以便我们将整个请求作为键值对(dict)获得。
user = Profile.objects.filter(email=posts['userEmailId']).first()