Python 条带API;如何在切换页面时传递参数?
我用的是StripeAPI,用的是烧瓶。我想知道在使用Python 条带API;如何在切换页面时传递参数?,python,flask,stripe-payments,Python,Flask,Stripe Payments,我用的是StripeAPI,用的是烧瓶。我想知道在使用重定向(url\u for(“”))切换页面时如何传递参数 比如说, @app.route('/charge', methods=['POST']) def charge(): amount= #Amount in cents customer = stripe.Customer.create( email='customer@example.com', source=request.for
重定向(url\u for(“”))
切换页面时如何传递参数
比如说,
@app.route('/charge', methods=['POST'])
def charge():
amount= #Amount in cents
customer = stripe.Customer.create(
email='customer@example.com',
source=request.form['stripeToken']
)
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Test'
)
当我输入完卡片信息后,页面切换到其他页面并创建令牌。现在我想从上一页得到钱的数量作为一个参数。但我不知道在使用StripeAPI时如何做到这一点
通常,我可以像这个答案那样做
我该怎么做
我尝试了第一个人的方法,但没有成功
上一页是这样的
在这个页面上,用户将使用条带签出窗口输入他们的卡信息,输入后,它将重定向到charge
页面
@app.route('/checkout', methods=['GET'])
def checkout():
item_id = request.args['item_id']
item = Item.query.filter_by(id=item_id).first()
return redirect(url_for('charge', item_id=item_id), code=307)
return render_template('checkout.html', title='Check Out',key=stripe_keys['publishable_key'], item=item)
当我进入此页面时,此错误消息出现在页面上:
错误的请求浏览器(或代理)发送了此服务器无法理解的请求
解决方案如下:
from flask import Flask, request
@app.route('/previouspage/', methods=['GET'])
def previous_page():
item = Item.query.filter_by(id=item_id).first()
redirect(url_for('charge', amount=item.price), code=307)
@app.route('/charge/<amount>', methods=['GET', 'POST'])
def charge(amount):
customer = stripe.Customer.create(
email='customer@example.com',
source=request.form['stripeToken']
)
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Test'
)
请记住,这两种方法都不是那么安全,因为有人可以在url中更改价格。因此,最好只传递项目ID,然后在/charge
中获取项目。像这样:
@app.route('/previouspage/', methods=['GET'])
def previous_page():
item = Item.query.filter_by(id=item_id).first()
return redirect(url_for('charge', amount=item.price), code=307)
@app.route('/charge', methods=['GET', 'POST'])
def charge():
amount = request.args.get('amount')
customer = stripe.Customer.create(
email='customer@example.com',
source=request.form['stripeToken']
)
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Test'
)
@app.route('/previouspage/', methods=['GET'])
def previous_page():
return redirect(url_for('charge', item_id=item_id), code=307)
@app.route('/charge', methods=['GET', 'POST'])
def charge():
amount = request.args.get('item_id')
item = Item.query.filter_by(id=item_id).first()
if not item:
return '....some kind of error'
customer = stripe.Customer.create(
email='customer@example.com',
source=request.form['stripeToken']
)
charge = stripe.Charge.create(
customer=customer.id,
amount=item.price,
currency='usd',
description='Test'
)
解决方案如下:
from flask import Flask, request
@app.route('/previouspage/', methods=['GET'])
def previous_page():
item = Item.query.filter_by(id=item_id).first()
redirect(url_for('charge', amount=item.price), code=307)
@app.route('/charge/<amount>', methods=['GET', 'POST'])
def charge(amount):
customer = stripe.Customer.create(
email='customer@example.com',
source=request.form['stripeToken']
)
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Test'
)
请记住,这两种方法都不是那么安全,因为有人可以在url中更改价格。因此,最好只传递项目ID,然后在/charge
中获取项目。像这样:
@app.route('/previouspage/', methods=['GET'])
def previous_page():
item = Item.query.filter_by(id=item_id).first()
return redirect(url_for('charge', amount=item.price), code=307)
@app.route('/charge', methods=['GET', 'POST'])
def charge():
amount = request.args.get('amount')
customer = stripe.Customer.create(
email='customer@example.com',
source=request.form['stripeToken']
)
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Test'
)
@app.route('/previouspage/', methods=['GET'])
def previous_page():
return redirect(url_for('charge', item_id=item_id), code=307)
@app.route('/charge', methods=['GET', 'POST'])
def charge():
amount = request.args.get('item_id')
item = Item.query.filter_by(id=item_id).first()
if not item:
return '....some kind of error'
customer = stripe.Customer.create(
email='customer@example.com',
source=request.form['stripeToken']
)
charge = stripe.Charge.create(
customer=customer.id,
amount=item.price,
currency='usd',
description='Test'
)
实际上我想传递的不是表单的一部分,所以我不能这样做。你想传递什么样的数据?JSON?即使是json,也可以执行request.get_json(),它不是表单。它只是数据表中列的一部分,并设置为变量。与上一页一样,
item=item.query.filter\u by(id=item\u id)。first()
item的定义如下,其中一列表示价格。我想从特定项中获取它,因此通常我可以使用url\u为
传递一个变量。首先,重定向url\u为
将返回302并移动到get
。因此,您需要的是这个重定向(url\u表示('charge',amount=item.price),code=307)
。同时更新代码实际上我想传递的不是表单的一部分,所以我不能这样做。你想传递什么样的数据?JSON?即使是json,也可以执行request.get_json(),它不是表单。它只是数据表中列的一部分,并设置为变量。与上一页一样,item=item.query.filter\u by(id=item\u id)。first()
item的定义如下,其中一列表示价格。我想从特定项中获取它,因此通常我可以使用url\u为
传递一个变量。首先,重定向url\u为
将返回302并移动到get
。因此,您需要的是这个重定向(url\u表示('charge',amount=item.price),code=307)
。同时更新代码