Python 在将Stripe与Flask集成时,如何防止表单提交到服务器?

Python 在将Stripe与Flask集成时,如何防止表单提交到服务器?,python,flask,forms,stripe-payments,Python,Flask,Forms,Stripe Payments,我一直遵循Stripe的教程,使用他们的API创建自己的表单。长话短说,当表单提交时,一点Javascript将数据发送到服务器,然后附加stripeToken输入字段。然后再次提交表单,我的Flask应用程序应该读取这个新表单字段的值。但是,当我尝试运行代码时,它会产生一个500内部服务器错误,我认为这是因为Flask正在立即查找表单stripeToken,它在第一次检查时不存在该表单。是否有Javascript或Python可以添加到不调用request.form['stripeToken'

我一直遵循Stripe的教程,使用他们的API创建自己的表单。长话短说,当表单提交时,一点Javascript将数据发送到服务器,然后附加
stripeToken
输入字段。然后再次提交表单,我的Flask应用程序应该读取这个新表单字段的值。但是,当我尝试运行代码时,它会产生一个500内部服务器错误,我认为这是因为Flask正在立即查找表单
stripeToken
,它在第一次检查时不存在该表单。是否有Javascript或Python可以添加到不调用
request.form['stripeToken']
,直到Stripe添加了它

以下是我的相关HTML:

<head>
    <title>Settings</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://js.stripe.com/v1/"></script>
    <script type="text/javascript">
    // This identifies your website in the createToken call below
    Stripe.setPublishableKey('STRIPE_TEST_KEY');

    var stripeResponseHandler = function(status, response) {
      var $form = $('#payment-form');

      if (response.error) {
        // Show the errors on the form
        $form.find('.payment-errors').text(response.error.message);
        $form.find('button').prop('disabled', false);
      } else {
        // token contains id, last4, and card type
        var token = response.id;
        // Insert the token into the form so it gets submitted to the server
        $form.append($('<input type="hidden" name="stripeToken" id="stripeToken" />').val(token));
        // and re-submit
        $form.get(0).submit();
      }
    };

    jQuery(function($) {
      $('#payment-form').submit(function(e) {
        var $form = $(this);

        // Disable the submit button to prevent repeated clicks
        $form.find('button').prop('disabled', true);

        Stripe.createToken($form, stripeResponseHandler);

        // Prevent the form from submitting with the default action
        return false;
      });
    });
  </script>
</head>

<body>
    <div id="payments">
        <h1>Add a credit card</h1>
        <form action="{{ url_for('stats') }}" method="POST" id="payment-form">
以及日志:

2013-04-23T01:57:39.590458+00:00 app[web.1]: 10.44.13.218 - - [2013-04-23 01:57:39] "GET /stats HTTP/1.1" 200 2348 0.014429
2013-04-23T01:57:39.590171+00:00 heroku[router]: at=info method=GET path=/stats host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=3ms service=19ms status=200 bytes=2230
2013-04-23T01:57:39.804162+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=1ms service=4ms status=404 bytes=238
2013-04-23T01:57:39.808201+00:00 app[web.1]: 10.127.113.186 - - [2013-04-23 01:57:39] "GET /favicon.ico HTTP/1.1" 404 347 0.000610
2013-04-23T01:57:54.934252+00:00 heroku[router]: at=info method=POST path=/stats host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=1ms service=24ms status=500 bytes=291
2013-04-23T01:57:54.931497+00:00 app[web.1]: 10.92.74.166 - - [2013-04-23 01:57:54] "POST /stats HTTP/1.1" 500 412 0.020936
2013-04-23T01:57:55.133776+00:00 app[web.1]: 10.44.58.92 - - [2013-04-23 01:57:55] "GET /favicon.ico HTTP/1.1" 404 347 0.000603
2013-04-23T01:57:55.137417+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=24ms service=43ms status=404 bytes=238
2013-04-23T01:57:58.710721+00:00 heroku[router]: at=info method=GET path=/stats host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=1ms service=15ms status=200 bytes=2230
2013-04-23T01:57:58.714328+00:00 app[web.1]: 10.44.45.210 - - [2013-04-23 01:57:58] "GET /stats HTTP/1.1" 200 2348 0.012623
2013-04-23T01:57:58.826291+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=1ms service=3ms status=404 bytes=238
2013-04-23T01:57:58.827469+00:00 app[web.1]: 10.44.13.218 - - [2013-04-23 01:57:58] "GET /favicon.ico HTTP/1.1" 404 347 0.000772
更新:这是在本地运行的回溯)

更新2:我推断在我的本地测试脚本上出现上述错误是因为我忘记了导入条带。这样做使它运行良好。然而,在Heroku上运行时,同样的代码继续产生500错误


更新3(启示录):所以我的Stripe仪表板上说我确实在创建新用户。因此,我认为公平的假设是,在(包括)
customer=stripe.customer.create
之前的所有代码都有效。那么,500错误是由我修改数据库引起的吗?

名称错误:未定义全局名称“stripe”


看起来你忘了导入stripe

在检查我的stripe仪表板后,我意识到客户创建是正确的。所以错误出现在下面的代码中,我在数据库中更新了用户的stripe_id。
问题是条带id的Flask列需要一个整数。但是,Stripe将id指定为字符串

您是否确实检查了Flask服务器的日志,以查看500ise实际上来自哪个Python错误?随便猜测原因可能会让你误入歧途。如果您已检查日志,请将错误复制粘贴到此处。@Amber是的,我已检查过–很抱歉,我忽略了发布它。我已经更新了我的问题。我没有看到错误消息,只有500个响应代码。@Amber我在本地运行了它(因此行号不同),但我已经用回溯更新了我的问题抱歉更新太多,但当我复制脚本在本地运行它时,这是一个打字错误。我刚刚检查了我的Stripe仪表板,它说我正在成功地为每个表单提交创建用户(尽管我仍然看到500个错误)。在这种情况下,我认为在调用
customer=stripe.customer.create
2013-04-23T01:57:39.590458+00:00 app[web.1]: 10.44.13.218 - - [2013-04-23 01:57:39] "GET /stats HTTP/1.1" 200 2348 0.014429
2013-04-23T01:57:39.590171+00:00 heroku[router]: at=info method=GET path=/stats host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=3ms service=19ms status=200 bytes=2230
2013-04-23T01:57:39.804162+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=1ms service=4ms status=404 bytes=238
2013-04-23T01:57:39.808201+00:00 app[web.1]: 10.127.113.186 - - [2013-04-23 01:57:39] "GET /favicon.ico HTTP/1.1" 404 347 0.000610
2013-04-23T01:57:54.934252+00:00 heroku[router]: at=info method=POST path=/stats host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=1ms service=24ms status=500 bytes=291
2013-04-23T01:57:54.931497+00:00 app[web.1]: 10.92.74.166 - - [2013-04-23 01:57:54] "POST /stats HTTP/1.1" 500 412 0.020936
2013-04-23T01:57:55.133776+00:00 app[web.1]: 10.44.58.92 - - [2013-04-23 01:57:55] "GET /favicon.ico HTTP/1.1" 404 347 0.000603
2013-04-23T01:57:55.137417+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=24ms service=43ms status=404 bytes=238
2013-04-23T01:57:58.710721+00:00 heroku[router]: at=info method=GET path=/stats host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=1ms service=15ms status=200 bytes=2230
2013-04-23T01:57:58.714328+00:00 app[web.1]: 10.44.45.210 - - [2013-04-23 01:57:58] "GET /stats HTTP/1.1" 200 2348 0.012623
2013-04-23T01:57:58.826291+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=ancient-oasis-5770.herokuapp.com fwd="99.110.189.136" dyno=web.1 connect=1ms service=3ms status=404 bytes=238
2013-04-23T01:57:58.827469+00:00 app[web.1]: 10.44.13.218 - - [2013-04-23 01:57:58] "GET /favicon.ico HTTP/1.1" 404 347 0.000772
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Python/2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/Zach/Desktop/t/t.py", line 20, in stats
    stripe.api_key = "STRIPE_TEST_KEY"
NameError: global name 'stripe' is not defined
127.0.0.1 - - [22/Apr/2013 22:11:03] "GET /stats?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [22/Apr/2013 22:11:03] "GET /stats?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [22/Apr/2013 22:11:03] "GET /stats?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [22/Apr/2013 22:11:03] "GET /stats?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [22/Apr/2013 22:11:03] "GET /stats?__debugger__=yes&cmd=resource&f=source.png HTTP/1.1" 200 -
127.0.0.1 - - [22/Apr/2013 22:11:03] "GET /stats?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -