Home » Python » python – Pass value to flask route when dropdown list change-Exceptionshub

python – Pass value to flask route when dropdown list change-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have a flask template rendered with a default value in the route function. I am trying to change the value by selecting an option in the dropdown list. I managed to get the selected value to javascript as follows.

html template (dropdown list part only)

<div class="dropdown-menu scrollable-menu">
    <a class="dropdown-item" data-attVal="1" href="#">Opt 1</a>
    <a class="dropdown-item" data-attVal="2" href="#">Opt 2</a>
<div>

javascript

$(".dropdown-menu a").click(function () {
  var selText = $(this).text();
  var selVal = $(this).attr("data-attVal");

  console.log(selText+":"+selVal);
});

app.py

@app.route('/', methods=['GET','POST'])
def display():
    i = 1
    # do many things based on value of i

    return render_template('display.html', response = data)

I don’t really need javascript to communicate with my flask app.py. That was my initial thought. But I have no idea how to proceed after this or make a call to route / from my html template. I need to change the value of i based on the dropdown list data-attVal.

How to&Answers: