简体   繁体   English

Flask:在整个应用程序中记住变量+ DOM操作

[英]Flask: remembering variables + DOM manipulation throughout application

I'm developing a Flask application that contains a ton of inputs. 我正在开发一个包含大量输入的Flask应用程序。 Some the inputs depend on other inputs so there is some DOM manipulation to expose these dependent inputs. 一些输入依赖于其他输入,因此有一些DOM操作来暴露这些依赖输入。 I would like for the application to remember the DOM state when the user goes back so the user doesn't have to reenter everything. 我希望应用程序在用户返回时记住DOM状态,这样用户就不必重新输入所有内容。 What's the best way to do this? 最好的方法是什么?

Below demonstrates what I think should work. 下面演示了我认为应该工作的内容。 I have an input box that takes a number. 我有一个带数字的输入框。 There is also a button that adds the same input field to the DOM. 还有一个按钮可以为DOM添加相同的输入字段。 When the user clicks submit, these inputs are appended to input_list and the list is displayed on the UI. 当用户单击“提交”时,这些输入将附加到input_list并且该列表将显示在UI上。 Because input_list is a global variable, the application will remember these inputs and their values (even if I go back in the browser or reload the page) - this is what I'm looking for. 因为input_list是一个全局变量,应用程序将记住这些输入及其值(即使我回到浏览器或重新加载页面) - 这就是我正在寻找的。

Again, I'm not sure if this is the best way to do it. 同样,我不确定这是否是最好的方法。 I know flask.g can be used to store application globals but I'm not sure this is the right use case for that (I've only seen that used to store database connections). 我知道flask.g可以用来存储应用程序全局,但我不确定这是否是正确的用例(我只看到用于存储数据库连接)。 I've also heard cookies may be useful for remembering changes in the DOM. 我也听说过cookie可能对记住DOM中的变化很有用。 Any thoughts/examples would be helpful and if anyone thinks my way is okay, I'd appreciate feedback on my code below. 任何想法/例子都会有所帮助,如果有人认为我的方式没问题,我会感谢下面对我的代码的反馈。

app.py: app.py:

@app.route('/input_page', methods=['GET', 'POST'])
def input_page():
    global input_list
    input_list = []
    if request.method == 'POST':
        if request.form.get('empty_list'):
            input_list = []
        elif request.form.get('submit_list'):
            input_list = [int(i) for i in request.form.getlist('x')]
    return render_template('input_page.html', input_list=input_list)

input_page.html: input_page.html:

<p>{{ input_list }}</p>

<form method="POST" action="/input_page">
  <div id="input_container">
    {% if input_list %}
      {% for i in input_list %}
        <input type="number" name="x" value="{{ input_list[loop.index0] }}">
      {% endfor %}
    {% else %}
      <input type="number" name="x">
    {% endif %}
  </div>
  <button type="button" id="add_input">Add Input</button>
  <input type="submit" value="Submit List" name="submit_list">
  <input type="submit" value="Empty List" name="empty_list">
</form>

<script>
  $(document).ready(function(){
    $('#add_input').click(function(){
      var $new_input = $('<input>', {type: 'number', name: 'x'});
      $('#input_container').append($new_input);
    });
  })
</script>

You seem to be on the right track. 你似乎走在了正确的轨道上。 However, I would shy away from a global variable because that essentially exposes the variable to other concurrent clients. 但是,我会回避全局变量,因为这实际上是将变量暴露给其他并发客户端。 I would however, recommend using the Flask session object: 但是,我建议使用Flask 会话对象:

from flask import Flask, session, ... # other flask modules you already have imported

Then in your Python code, you'd use: 然后在Python代码中,您将使用:

def input_page():
    if request.method == 'POST':
        if request.form.get('empty_list'):
            session['input_list'] = []
        elif request.form.get('submit_list'):
            session['input_list'] = [int(i) for i in request.form.getlist('x')]
    input_list = session.get('input_list', False)  # create an input_list var that either grabs the 'input_list' object from the session object or defaults to False if no key found
    return render_template('input_page.html', input_list=input_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM