【问题标题】:retrieve amount from database flask从数据库烧瓶中检索金额
【发布时间】:2020-10-27 03:12:26
【问题描述】:

我正在尝试创建条带结帐,但是当我尝试从数据库中获取金额或在会话中存储金额时,结帐不会重定向到条带结帐页面。

    results=Cart.query.filter_by(username = current_user.username, session_id = session['coded'])
    total = sum([cart.amount for cart in results])

但是如果我用整数替换总数,它就可以工作。查询非常准确。怎么了?

@posts.route('/checkout')
def checkout():
    results=Cart.query.filter_by(username = current_user.username, session_id = session['coded'])
    total = sum([cart.amount for cart in results])
    
    session = stripe.checkout.Session.create(
        payment_method_types=['card'],
        line_items=[{
          'price_data': {
            'currency': 'usd',
            'product_data': {
            'name': 'T-shirt',
            },
            'unit_amount':  total,
          },

            #'price': 'price_1GznR1BlIKnRHDeI80wgnj0A',
            'quantity': 1,
        }],
        mode='payment',
        success_url=url_for('posts.processing', _external=True) + '?session_id={CHECKOUT_SESSION_ID}',
        cancel_url=url_for('posts.viewcart', _external=True),
    )

【问题讨论】:

  • 在控制台/终端中运行时不会收到错误消息吗?你有没有使用print() 来检查你在 viarlabless 中得到了什么?
  • 是的,我做到了.....但是在控制台中它显示终端错误...我已经上传了问题中的错误
  • 错误解释所有 - 如果你想将 (session = ...) 分配给外部变量,那么你必须使用 global 来通知函数它必须分配给外部变量而不是创建局部变量。

标签: python flask stripe-payments


【解决方案1】:

你需要执行你的查询:

    results=Cart.query.filter_by(username = current_user.username, session_id = session['coded']).all()

【讨论】:

    【解决方案2】:

    我将会话声明为全局。我确保金额是整数

    @posts.route('/checkout')
    def checkout():
        global session
        results=Cart.query.filter_by(username = current_user.username, session_id = session['coded'])
        #results=Cart.query.filter_by(username = current_user.username, session_id = session['coded']).all()
        total = int(sum([cart.amount for cart in results]))
    
        print(results)
        session = stripe.checkout.Session.create(
            payment_method_types=['card'],
            line_items=[{
              'price_data': {
                'currency': 'usd',
                'product_data': {
                'name': 'T-shirt',
                },
                'unit_amount':  total,
              },
    
                #'price': 'price_1GznR1BlIKnRHDeI80wgnj0A',
                'quantity': 1,
            }],
            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=current_app.config['STRIPE_PUBLIC_KEY'])
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-29
      • 2021-11-04
      • 1970-01-01
      • 2017-09-03
      • 2020-07-10
      • 1970-01-01
      • 2021-06-15
      • 2019-11-24
      相关资源
      最近更新 更多