0

I trying to pass selected item from the dropdown list, when user click on submit button.

Here is below code in 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>

Here is the python code

@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()

getting error werkzeug.exceptions.BadRequestKeyError. Thanks for the help

miu
  • 189
  • 2
  • 13

1 Answers1

0

Your definition of a <select> tag is wrong. Its name needs to match the key you look up.

<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>
davidism
  • 121,510
  • 29
  • 395
  • 339
brillenheini
  • 793
  • 7
  • 22
  • thanks @mosesontheweb. this probably stupid question, but how to send the data from the dropdown selection to another file or execute python script. – miu Jul 16 '19 at 00:44