【问题标题】:Python Flask Select Dropdown item submit button [duplicate]Python Flask选择下拉项目提交按钮[重复]
【发布时间】:2019-11-23 21:30:42
【问题描述】:

当用户单击提交按钮时,我试图从下拉列表中传递所选项目。

下面是 HTML 代码

  <form action="/switchinfo" method="post">
    <div align="center" id="mainselection">
      <select name="sites" method="GET" action="/">
        <option selected="selected">Select an the site</option>
        {% for site in sites[0:] %}
        <option value="{{site}}">{{site}}</option>
        {% endfor %}
      </select>
    </div>
    <button class="button execute" name="submit" value="submit">Submit</button>
  </form>

这是python代码

@app.route('/', methods=['GET', 'POST'])
def index():
    sites = ['DC1', 'DC2', 'DC3', 'DC4']
    return render_template('index.html', sites=sites)

@app.route('/switchinfo', methods=['POST'])
def switchinfo():
    gather = request.form["site"]
    return render_template("switchinfo.html",gather=gather)

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

收到错误werkzeug.exceptions.BadRequestKeyError。 感谢您的帮助

【问题讨论】:

    标签: python forms button flask


    【解决方案1】:

    您对&lt;select&gt; 标签的定义是错误的。它的名称需要与您查找的键相匹配。

    <form action="/switchinfo" method="post">
        <div align="center" id="mainselection">
          <select name="site">
            <option selected>Select an the site</option>
            {% for site in sites %}
            <option value="{{site}}">{{site}}</option>
            {% endfor %}
          </select>
        </div>
        <button class="button execute" name="submit" value="submit">Submit</button>
      </form>
    

    【讨论】:

    • 感谢@mosesontheweb。这可能是愚蠢的问题,但是如何将数据从下拉选择发送到另一个文件或执行 python 脚本。
    猜你喜欢
    • 2016-12-07
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多