【问题标题】:How to store and collect a variable in Python script using Google App Engine?如何使用 Google App Engine 在 Python 脚本中存储和收集变量?
【发布时间】:2020-07-16 18:31:19
【问题描述】:

我正在创建一个表单,允许用户查找动物名称,并使用每个按钮(“添加动物”、“完成”)运行特定功能。

“添加动物”运行函数meta2SQL,它创建一个变量alg_id。目前这是一个环境变量,并不理想,因为这样一次只有一个用户可以填写表单而不会造成问题。

'Finish' 运行函数animal2SQL,它收集变量alg_id 并继续使用它。现在的问题是alg_id 没有被正确存储,收集和打印id 得到None

  1. 是什么导致 id 无法正确保存或收集,将其值设为 None
  2. 存储alg_id 并收集它的最佳方法是什么,以便许多用户可以同时填写表单,从而导致 id 与用户编号混淆?

HTML sn-p,其中包括我的 JS 代码:

<div class="section-25">
  <div class="container-5 w-container">
    <div class="text-block-6">Select the level of algorithm you&#x27;re looking to make</div>
    <div class="w-form">
      <form id="wf-form-Email-Form" name="wf-form-Email-Form" data-name="Email Form" method="post" action="">

        <!-- PAGE 1 -->
        <div id="page1" class="page" style="visibility:visible;">

          <!-- ALGORITHM NAME -->

          <label for="Algorithm-Name-3" class="custom-question algorithm-name">What will you name your algorithm?<br></label><input type="text" class="text-field enter-name w-input" maxlength="256" name="Algorithm-Name" data-name="Algorithm Name"
            placeholder="Be as creative as you like!" id="Algorithm-Name">

          <!-- ALGORITHM DESCRIPTION -->

          <label for="Algorithm-Desc-3" class="custom-question algorithm-desc">Briefly describe what your algorithm does?<br></label><input type="text" class="text-field enter-name w-input" maxlength="256" name="Algorithm-Description"
            data-name="Algorithm Description" placeholder="You can still be creative!" id="Algorithm-Desc">

          <p><input type="submit" class="submit-button-2 w-button" id="C1" value="Add Animal" onClick="showLayer('page2'), go_to_top_of_page()"></p>

        </div>


        <script>
          mybutton = document.getElementById("C1");

          function go_to_top_of_page() {
            form.action = "/meta2sql";
            form.submit();
          }
        </script>

        <!-- PAGE 2 (1st ANIMAL) -->

        <div id="page2" class="page">

          <p style="font-family: Poppins,sans-serif; color: #fff;">1st Animal</p>

          <!-- 1ST ANIMAL NAME -->

          <label for="Enter-species" class="custom-question enter-species" id="one_name">What animal are you looking for?</label>
          <input type="text" class="text-field w-input" maxlength="256" name="species1" placeholder="Enter name of animal" id="Enter-species" required="">

          <br><br>

          <p><input type="button" class="submit-button-2 w-button" id="B1" value="Go Back" onClick="showLayer('page1')">
            <input type="button" class="submit-button-2 w-button" id="F1" value="Finish" onClick="showLayer('page22'), animalSQL()"></p>
        </div>
      </form>
    </div>
  </div>
</div>


<!-- JAVASCRIPT -->

<script language="JavaScript">
  var currentLayer = 'page1';

  function showLayer(lyr) {
    hideLayer(currentLayer);
    document.getElementById(lyr)
      .style.visibility = 'visible';
    currentLayer = lyr;
  }

  function hideLayer(lyr) {
    document.getElementById(lyr).
    style.visibility = 'hidden';
  }

  function showValues(form) {
    var values = '';
    var len = form.length - 1;
    //Leave off Submit Button
    for (i = 0; i < len; i++) {
      if (form[i].id.indexOf("C") != -1 ||
        form[i].id.indexOf("B") != -1)
        //Skip Continue and Back Buttons
        continue;
      values += form[i].id;
      values += ': ';
      values += form[i].value;
      values += '\n';
    }
    alert(values);
  }
</script>


<script>
  form = document.getElementById("wf-form-Email-Form");

  function animalSQL() {
    form.action = "/animal2sql";
    form.submit();
  }
</script>

python sn-p,包括这个过程中涉及到的两个函数

from flask import Flask, request, render_template, url_for, redirect

app = Flask(__name__)

@app.route("/")
def index():
    return render_template('index.html')
    
@app.route('/meta2sql', methods=['GET', 'POST'])
def meta2sql():
    alg_name = request.form['Algorithm-Name']
    alg_description = request.form['Algorithm-Description']
    alg_metadata = mysql.meta(alg_name,alg_description)
    os.environ['alg_id']=str(alg_metadata['alg_id'][0])
    return '', 204

@app.route('/animal2sql', methods=['GET', 'POST'])
def animal2sql():
    k = 20
    while k:
        if request.form['species{}'.format(k)]:
            common = request.form['species{}'.format(k)]
            break
        else:
            k = k-1

    db_user = 'root'
    db_pass = 'whalesrule!!'
    db_name = 'algorithm_library'
    db_ip   = '10.103.128.3'
    id = os.getenv('alg_id')
    print(id)
    # And then I do some things with id 
    return '', 204

if __name__ == "__main__":
    app.debug = True
    app.run()

感谢任何建议

【问题讨论】:

    标签: python forms google-app-engine flask environment-variables


    【解决方案1】:

    您不应该在os.env 中存储动态变量。这些旨在在您的应用程序启动时读取和存储。这是一个简单的数据库存储变量的工作。如果您的应用程序如此简单,您可以读取和写入内存缓存,但这不会持续存在。使用简单的数据库。

    【讨论】:

    • 这是个好建议;您能否详细说明为什么 memcache 不持久?
    • 它只是存储在 RAM 中。每当需要回收这些资源时,它就会消失。它通常可以持续几天,但不能保证。它可以随时消失,尤其是它没有被访问
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多