0

For some reason I've been struggling with this, I've made all sorts of attempts and searched a bunch of answers but nothing seems to be working correctly.

Question: There are two buttons on a page that hit two separate endpoints (flask-admin), however the first button (Save button) is always submitted regardless of which button I select. How do I prevent the first button (Save) from executing when I hit the second button (Edit)? What am I doing wrong here?

Note: I would prefer to do this with HTML alone. If there is an answer already out there that I haven't seen I will happily delete this question -- I landed on my current implementation from this answer: Two submit buttons in one form. I just need a little help because I've spent a whole day on this already. Thank you!

Edit: Please please please NO PHP. Thanks :)

 <h3> Backtest ID: {{ backtestID }} </h3>
 <div class="row">

     <div class="col-md-1" >
         <form method="post">
             <button type="submit" class="btn btn-success" 
                     formaction="{{ url_for('results.save_backtest', id=backtestID) }}">Save</button>
         </form>
     </div>

     <div class="col-md-1" >
         <form method="post">
             <button type="submit" class="btn btn-info" 
                     formaction="{{ url_for('results.rerun_backtest', id=backtestID) }}">Run</button>
         </form>
     </div>

 </div>


mrp
  • 689
  • 2
  • 11
  • 28

1 Answers1

1

Delete the type="submit" attribute. This attribute let's the form send.

 <h3> Backtest ID: {{ backtestID }} </h3>
 <div class="row">

     <div class="col-md-1" >
         <form method="post">
             <button class="btn btn-success" 
                     formaction="{{ url_for('results.save_backtest', id=backtestID) }}">Save</button>
         </form>
     </div>

     <div class="col-md-1" >
         <form method="post">
             <button class="btn btn-info" 
                     formaction="{{ url_for('results.rerun_backtest', id=backtestID) }}">Run</button>
         </form>
     </div>

 </div>
Aaron Junker
  • 127
  • 1
  • 14
  • Unbelievable. Yes this works. Would you have any insight as to _why_ this works? – mrp Oct 21 '21 at 14:02
  • Actually, this doesn't work. Now the second button is only firing. This makes no sense. – mrp Oct 21 '21 at 23:25
  • I ended up using input tags instead of button tags and used one endpoint instead of multiple. Where the one endpoint uses conditional IF statements to find which input was submitted. – mrp Oct 26 '21 at 12:59