如何在Python中获取Javascript onSuccess响应对象变量?
背景: 在Django应用程序中,我有一个使用外部API执行操作(生成付款)的模板。API调用是在Javascript中完成的 成功时,API返回一个如何在Python中获取Javascript onSuccess响应对象变量?,python,django,django-models,django-templates,Python,Django,Django Models,Django Templates,背景: 在Django应用程序中,我有一个使用外部API执行操作(生成付款)的模板。API调用是在Javascript中完成的 成功时,API返回一个响应对象。这在测试中效果很好 function makePayment(applicationAmount) { let paymentEngine = RmPaymentEngine.init({ key: 'abcd' firstName: '{{ user.f_name }}', onS
响应
对象。这在测试中效果很好
function makePayment(applicationAmount) {
let paymentEngine = RmPaymentEngine.init({
key: 'abcd'
firstName: '{{ user.f_name }}',
onSuccess: function(response) {
console.log('callback Successful Response', response);
// TODO: Add a function to post the payment records to the database
// response from the API contains a dictionary like (read json) object
// how can I get that response object in Python and use those response
// object values to update a django model database records
},
onError: function(response) {
console.log('callback Error Response', response);
// TODO: Add a function to throw an error message
},
onClose: function() {
console.log("closed");
}
});
}
问题:
如何在Python中获取onSuccess
response
对象变量,并使用这些response
对象值来更新django模型数据库记录
查看此链接:但似乎无法实现我的需求
我很高兴被引导到一个简单的资源来解释这个过程,而不是一些需要花费数小时才能理解的非常复杂的东西。使用@Lag11和@Arount的想法和一个精彩的教程,我创建了两个基于函数的视图,一个用于服务“页面”,另一个用于处理“页面发布到数据库” 总之,我是在模板中完成的:
function makePayment(applicationAmount) {
let paymentEngine = RmPaymentEngine.init({
key: 'abcd'
firstName: '{{ user.f_name }}',
onSuccess: function(response) {
console.log('callback Successful Response', response);
// start new additions
data = {
"payer_email": "{{ user.email }}",
"payer_phone": "{{ user.phone }}",
"payment_amount_total": response.amount,
}
$.ajax({
type: 'POST',
url: "{% url 'post_purchase_form' %}",
data: data,
onSuccess: function (response) {
console.log('callback db post Successful', response);
},
error: function (response) {
// alert the error if any error occured
alert(response);
}
})
// end new additions
},
onError: function(response) {
console.log('callback Error Response', response);
// TODO: Add a function to throw an error message
},
onClose: function() {
console.log("closed");
}
});
}
def application_form_view(request):
user = request.user
form = ApplicationForm(instance=user)
return render(request, 'application_form.html', {'form': form, 'user': user})
def post_application_form_view(request):
# request should be ajax and method should be POST.
if request.is_ajax and request.method == "POST":
# get the form data
form = ApplicationForm(request.POST, request.FILES)
# save the data and after fetch the object in instance
if form.is_valid():
instance = form.save()
# serialize in new FormPurchase object in json
ser_instance = serializers.serialize('json', [ instance, ])
# send to client side.
return JsonResponse({"instance": ser_instance}, status=200)
else:
# some form errors occured.
return JsonResponse({"error": form.errors}, status=400)
# some error occured
return JsonResponse({"error": "unknown errors"}, status=400)
在视图.py中:
function makePayment(applicationAmount) {
let paymentEngine = RmPaymentEngine.init({
key: 'abcd'
firstName: '{{ user.f_name }}',
onSuccess: function(response) {
console.log('callback Successful Response', response);
// start new additions
data = {
"payer_email": "{{ user.email }}",
"payer_phone": "{{ user.phone }}",
"payment_amount_total": response.amount,
}
$.ajax({
type: 'POST',
url: "{% url 'post_purchase_form' %}",
data: data,
onSuccess: function (response) {
console.log('callback db post Successful', response);
},
error: function (response) {
// alert the error if any error occured
alert(response);
}
})
// end new additions
},
onError: function(response) {
console.log('callback Error Response', response);
// TODO: Add a function to throw an error message
},
onClose: function() {
console.log("closed");
}
});
}
def application_form_view(request):
user = request.user
form = ApplicationForm(instance=user)
return render(request, 'application_form.html', {'form': form, 'user': user})
def post_application_form_view(request):
# request should be ajax and method should be POST.
if request.is_ajax and request.method == "POST":
# get the form data
form = ApplicationForm(request.POST, request.FILES)
# save the data and after fetch the object in instance
if form.is_valid():
instance = form.save()
# serialize in new FormPurchase object in json
ser_instance = serializers.serialize('json', [ instance, ])
# send to client side.
return JsonResponse({"instance": ser_instance}, status=200)
else:
# some form errors occured.
return JsonResponse({"error": form.errors}, status=400)
# some error occured
return JsonResponse({"error": "unknown errors"}, status=400)
您不能将Javascript对象传递给Python,它是两种不同的语言,一种在用户浏览器中运行,另一种在服务器上运行。当支付成功时,处理某件事情的最佳方法是在
OnSuccess
函数(Javascript)中调用内部API(Python),并通过POST或GET参数将response
(Javascript)中的预期参数传递给Python API。在OnSuccess函数中,使用ajax将响应发送到后端,以处理您希望使用pythonThanks@Arount执行的操作。你对我如何实现这个或一个例子有指导吗?