Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Flask/Javascript中向服务器获取动态列表元素_Javascript_Python_Flask - Fatal编程技术网

在Flask/Javascript中向服务器获取动态列表元素

在Flask/Javascript中向服务器获取动态列表元素,javascript,python,flask,Javascript,Python,Flask,我正在尝试编写一个非常基本的Flask/Javascript应用程序,它可以在每次单击“提交”按钮时检查元素列表的顺序(并将其发送到服务器);我尝试了许多不同的“request.get”变体,但似乎找不到任何可以在listWithHandle元素上提取任何信息的东西 这似乎是一个简单的操作,但我对网络编程非常陌生,我还无法解码我确信包含解决方案的文档 有两个文件: app.py from flask import Flask, render_template, request, redirect

我正在尝试编写一个非常基本的Flask/Javascript应用程序,它可以在每次单击“提交”按钮时检查元素列表的顺序(并将其发送到服务器);我尝试了许多不同的“request.get”变体,但似乎找不到任何可以在listWithHandle元素上提取任何信息的东西

这似乎是一个简单的操作,但我对网络编程非常陌生,我还无法解码我确信包含解决方案的文档

有两个文件:

app.py

from flask import Flask, render_template, request, redirect
app = Flask(__name__)

names = ['Ivuoma', 'Carla', 'Aly']

@app.route('/')
def hello_world():
    # When "submit" button is clicked, 
    # print order of names to console,
    # then reorder python names list and render_template.
    return render_template('index.html', names=names)

if __name__ == '__main__':
    app.run(debug=True)
prelim_names = ['Carla', 'Aly', 'Ivuoma']
@app.route('/', methods=['GET', 'POST'])
def hello_world():
    names = request.form.getlist('handles[]')
    if not names:
        names = prelim_names
    print('names to display', names)
    return render_template('index.html', names=names)
index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Sortable Test!</title>
  </head>
  <body>

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/>
  <script src="http://rubaxa.github.io/Sortable/Sortable.js"></script>

  <ul id="listWithHandle">
      {% for name in names %}
        <li><span class="my-handle">:+:</span>{{ name }}</li>
      {% endfor %}
  </ul>

  <div>
      <button class="submit">Submit</button>
  </div>

  <script id="sortable-script">
        Sortable.create(listWithHandle, {
          handle: '.my-handle',
          animation: 150
        });
  </script>
  </body>
</html>
index.html

  <form method="post">
  <ul id="listWithHandle">
    {% for name in names %}
        <li>
            <span class="my-handle">:+:</span>
            <input type="hidden" name="handles[]" value="{{ name }}"/> {{ name }}
        </li>
    {% endfor %}
  </ul>


    <div>
        <button class="btn btn-lg btn-primary">Submit</button>
    </div>
  </form>

    {名称中的名称为%}
  • :+: {{name}}
  • {%endfor%}
提交
{%for name in names%}
  • :+:{{name}
  • {%endfor%}

    您需要提供
    ,以便发送数据。

    的粗不。。。你需要提供价值观。。。对于request.args和request.form
    {% for name in names %}
    <li><span class="my-handle">:+:</span><input type="hidden" name="handles[]" value="{{ name }}"/>{{ name }}</li>
    {% endfor %}