Flask passing the value from a clicked button

I am currently using Flask and python. I have the following code for the html file:

<body>
  {% for result in rest_info %}
  <div class="cotainer">
    <button class="btn btn-primary" type="submit" id="{{result2}}">{{result[2]}}</button>
  </div>
  {% endfor %}
  </body>

I would like to transition to a different html based on the button clicked. I wrote jQuery script something like this to get the value of the button pressed:

$(document).ready(function() {
   $(this).click(function() {
       var stored_value = $(this).id;
   });
});

I am wondering how I can pass stored_value to a view function in Flask python... Thanks for your help! EDIT: the list of buttons is arbitrarily long!

Answers 1

  • To get something from the browser to a view function, you're going to need to make a request. There are lots of ways to make this happen, like clicking an <a> tag, submitting a <form>, or having jQuery do location = '?value=2';.

    You can probably solve this problem without JavaScript. Consider the following template:

    {% if request.args.blah != '1' %}
    <a href="/?blah=1">Result</a>
    <p>The regular HTML</p>
    {% else %}
    <a href="/">Result</a>
    <p>This HTML is different!</p>
    {% endif %}
    

Related Articles