How To Access The Selected Dropdown Back To Flask
I have been trying to access the selected dropdown back to the Flask on a click of button. Based on one of the suggestions I did as follows app.py @app.route('/ra/connect',meth
Solution 1:
I can't get your error with your code. Probably you get error for different code but you didn't show full error message which could confirm it.
To update HTML
without reloading page you need JavaScript
or jQuery
to send AJAX
request, get response and put it in existing HTML
Minimal working example:
from flask import Flask, request, render_template_string
app = Flask(__name__)
@app.route('/')
def index():
return render_template_string('''<html>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<form action="/ra/connect" method="POST" id="form">
<select name="selected_class" class="form-control" id="all_classes">
{% for o in all_classes %}
<option value="{{ o }}" selected>{{ o }}</option>
{% endfor %}
</select>
<button class="form-control" id="button">Get gateways</button>
</form>
<p id="countdown"></p>
<script>
$('#button').click( function(event) {
event.preventDefault();
$.post("/ra/connect", $('#form').serialize(), function(data) {
//alert(data);
countdown = $("#countdown");
countdown.append(data + "<br/>");
});
});
</script>
</html>''', all_classes=['Hello', 'World'])
@app.route('/ra/connect', methods=['GET', 'POST'])
def connect_management():
user = request.form.get('selected_class')
print('user:', user)
return str(user)
app.run()
BTW: Continuation in flask variable access from HTML
Post a Comment for "How To Access The Selected Dropdown Back To Flask"