0

I am very new to the flask, I am creating an application where I need to send selected value to my python code but to different routes after submit.

For Example -- I have 2 select boxes in my application now when I select an option from select box1 the selected value should go to the route "route1" and when I select an option from select box2 the selected value should go to the route "route2".

Below Is my code--

Python

app = Flask(__name__)
@app.route('/')
def index():
  return render_template(
   'example.html',
    data=[{'name':'One Hour'}, {'name':'Three Hour'}, {'name':'Six 
    Hour'}, {'name':'Twelve Hour'}, {'name':'One Day'}])


@app.route("/test" , methods=['GET', 'POST'])
def test():
  select = request.form.get('comp_selectbox1')
  ##my code
  return render_template()

@app.route("/Foo" , methods=['GET', 'POST'])
def Foo():
  select = request.form.get('comp_selectbox2')
  ##my code
  return render_template()

if __name__=='__main__':
  app.run(debug=False, threaded=True)

HTML

<form method="POST" action="{{ url_for('test') }}">
<form method="POST" action="{{ url_for('Foo') }}">

<div class="form-group">
    <select name="comp_selectbox1" id="comp_selectbox1" 
    class="selectpicker form-control" style = "width:150px;background-
    color:#3b8ec2;font-color:white;border:3px solid 
    #336600;padding:10px;font-size:10pt">
      <option selected>Select test</option>
      <option value="Some Link">One Hour</option>
      <option value="Some Link">Three Hour</option>
      <option value="Some Link">Six Hour</option>
      <option value="Some Link">Twelve Hour</option>
      <option value="Some Link">One Day</option>
    </select>
    <select name="comp_selectbox2" id="comp_selectbox2" 
    class="selectpicker form-control" style = "width:150px;background-
    color:#3b8ec2;font-color:white;border:3px solid 
    #336600;padding:10px;font-size:10pt" >
      <option selected>Select Foo</option>
      <option value="Some Link">One Hour</option>
      <option value="Some Link">Three Hour</option>
      <option value="Some Link">Six Hour</option>
      <option value="Some Link">Twelve Hour</option>
      <option value="Some Link">One Day</option>
    </select>
  <br></br>
<center>
<button type="submit" class="btn btn-default"  style="font-
size:15pt;color:black;
background-color:green;border:3px solid #336600;padding:10px" 
>Submit</button>
</center>
</div>
</form>

Some Link -- Link which I want in function request.form.get(). The problem is Whenever I am running my application and select value from selectbox2 the value is going to the first root defined in HTML.

<form method="POST" action="{{ url_for('test') }}">  

Help Appreciated!!

  • Please read about two forms: https://stackoverflow.com/q/18290142/5378816 – VPfB May 05 '18 at 13:50
  • You have malformated HTML code.. Two opening `
    ` tags and only one closing one. It would save headaches to just keep one form and have a third route to deal with form logic and which route to redirect to, test or Foo.
    – Mangohero1 May 06 '18 at 21:51

0 Answers0