【问题标题】:add python list values to input Flask将 python 列表值添加到输入 Flask
【发布时间】:2020-09-11 19:16:00
【问题描述】:

如果没有任何js 库,我必须怎么做才能将我从 python 列表中选择的值添加到输入? 我需要用逗号分别添加它们。问题是我可以选择一个值,不能第二次做,所以我有循环来做,但它不起作用。

app.py:

import csv
from flask import Flask, jsonify, make_response, render_template, request,

with open('templates/Testing.csv') as f:
        reader = csv.reader(f)
        values = next(reader)
        values = values[:len(values)-1]

@app.route('/', methods=['GET'])
def page_show():
    return render_template('includes/default.html', values=values)

if __name__ == '__main__':
    app.run()

default.html:

    <input type="text" id="values" list="val" multiple="multiple" class="searchTerm" name="value" placeholder="Type your values">
    <select list="values" name="values">
    {% for val in values %}
      <option value="{{val}}" SELECTED>{{val}}</option>
    {% endfor %}
  </select>
    <button type="submit" class="searchButton" name=form>
      <i class="fa fa-search"></i>
    </button>

如何解决?谢谢。

【问题讨论】:

    标签: python html forms flask


    【解决方案1】:

    得到错误/(ㄒoㄒ)/~~

    NameError: name 'values' is not defined
    

    像 app.run() 一样修复 from flask import Flask,render_template

    app = Flask(__name__)
    
    
    @app.route('/', methods=['GET'])
    def page_show():
        values = [1, 2, 3, 4, 5]
        print(values)
        return render_template('default.html', values=values)
    
    app.run()
    
    
    
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body>
        <input
          type="text"
          id="values"
          list="val"
          multiple="multiple"
          class="searchTerm"
          name="value"
          placeholder="Type your values"
        />
        <select list="values" name="values">
          {% for val in values %}
          <option value="{{val}}" selected>{{val}}</option>
          {% endfor %}
        </select>
        <button type="submit" class="searchButton" name="form">
          <i class="fa fa-search"></i>
        </button>
      </body>
    </html>
    

    成功了!

    【讨论】:

    • 它不起作用。请查看我更新的代码以消除错误。
    • 它工作...从烧瓶导入 csv import Flask,render_template app = Flask(name) with open(r'demo\templates\Testing.csv') as f : reader = csv.reader(f) values = next(reader) values = values[:len(values)-1] @app.route('/', methods=['GET']) def page_show(): values = [1, 2, 3, 4, 5] print(values) return render_template('default.html', values=values) app.run()
    • 在您的代码中您可以选择值,但不能再次选择另一个值。我也需要选择另一个并将其添加到我可以从中选择值的同一输入中/
    • 你可以查看这个link来完全理解它。
    【解决方案2】:

    ( ̄▽ ̄)"

    import csv
    from flask import Flask,render_template
    
    app = Flask(__name__)
    with open(r'demo\templates\Testing.csv') as f:
            reader = csv.reader(f)
            values = next(reader)
            values = values[:len(values)-1]
    
    @app.route('/', methods=['GET'])
    def page_show():
        values = [1, 2, 3, 4, 5]
        print(values)
        return render_template('default.html', values=values)
    
    app.run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-11
      • 2018-07-26
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      相关资源
      最近更新 更多