Python 在条带签出中,如何使用循环在flask中显示数据库中的多个产品
我有问题循环产品(显示多个产品)在条纹结帐在烧瓶。正常情况下,它应该能工作。但是当循环时,它只显示一个产品,而不是循环的所有产品。我不知道我做错了什么。代码如下所示:Python 在条带签出中,如何使用循环在flask中显示数据库中的多个产品,python,flask,stripe-payments,Python,Flask,Stripe Payments,我有问题循环产品(显示多个产品)在条纹结帐在烧瓶。正常情况下,它应该能工作。但是当循环时,它只显示一个产品,而不是循环的所有产品。我不知道我做错了什么。代码如下所示: @posts.route('/checkout') def checkout(): res=Ct.query.filter_by(username = current_user.username, status='pending').order_by(Ct.id.desc()).all() #course curr
@posts.route('/checkout')
def checkout():
res=Ct.query.filter_by(username = current_user.username, status='pending').order_by(Ct.id.desc()).all() #course current user intends to buy
for r in res:
te = {
'price_data': {
'currency': 'usd',
'unit_amount_decimal': total,
'product_data': {
'name': r.course,
'images': ['https://i.imgur.com/EHyR2nP.png'],
},
},
'quantity': 1,
}
session = stripe.checkout.Session.create(
payment_method_types=['card'],
line_items=[te,],
payment_intent_data={
'application_fee_amount': fee,
'transfer_data': {
'destination': account_id,
},
},
metadata={'order_id': '6735'},
customer_email='a@b.c',
mode='payment',
success_url=url_for('posts.processing', _external=True) + '?session_id={CHECKOUT_SESSION_ID}',
cancel_url=url_for('posts.viewcart', _external=True),
)
return jsonify(checkout_session_id=session['id'], checkout_public_key=config.get('STRIPE_PUBLIC_KEY'))
现在,您正在创建一个签出会话,同时在
res
列表中循环。您可能需要做的是生成一个行项目数组,然后在创建会话时将该数组传递给line\u items
属性。尝试将会话创建调用移到for循环之外,并按照此处的链接获取一个关于如何在Python中附加到循环中的数组的示例:它起作用了。非常感谢你的提示。我真的很感激。。。。。