I'm trying to retrieve the selected option on a dynamic dependent dropdown list, but I only get None using request.form.get. My example generates a dropdown list in the image below:
The full code to generate the dropdown list is below, and it is based on an answer to this question.
from flask import Flask, render_template_string, request, redirect
app = Flask(__name__)
@app.route('/', methods=["GET", "POST"])
def index():
systems = {
'PlayStation': {'Spyro': ['1', '2', '3'], 'Crash': ['4', '5', '6'], 'Ico': ['7', '8']},
'N64': {'Mario Kart': ['Mario', 'Luigi'], 'Superman': ['12', '13', '14']}
}
if request.method == "POST":
systemGet = request.form.get("system")
gameGet = request.form.get("game")
charactersGet = request.form.get("characters")
print(systemGet)
print(gameGet)
print(charactersGet)
return redirect('temp.html')
return render_template_string(template, systems=systems)
template = """
<!doctype html>
<form method="POST">
<select id="system">
<option></option>
</select>
<select id="game"></select>
<select id="characters"></select>
<button type="submit">Play</button>
</form>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
"use strict";
var systems = {{ systems|tojson }};
var form = $('form');
var system = $('select#system');
var game = $('select#game');
var characters = $('select#characters');
for (var key in systems) {
system.append($('<option/>', {'value': key, 'text': key}));
}
system.change(function(ev) {
game.empty();
game.append($('<option/>'));
var games = systems[system.val()];
for (var i in games) {
game.append($('<option/>', {'value': i, 'text': i}));
}
});
game.change(function(evV) {
characters.empty();
characters.append($('<option/>'));
var games = systems[system.val()][game.val()];
for (var i in games) {
characters.append($('<option/>', {'value': games[i], 'text': games[i]}));
}
});
</script>
"""
if __name__ == '__main__':
app.run()
Edit:
Including the name attribute, as proposed by @vremes, all works. The solution becomes:
from flask import Flask, render_template_string, request, redirect
app = Flask(__name__)
@app.route('/', methods=["GET", "POST"])
def index():
systems = {
'PlayStation': {'Spyro': ['1', '2', '3'], 'Crash': ['4', '5', '6'], 'Ico': ['7', '8']},
'N64': {'Mario Kart': ['Mario', 'Luigi'], 'Superman': ['12', '13', '14']}
}
if request.method == "POST":
systemGet = request.form.get("system")
gameGet = request.form.get("game")
charactersGet = request.form.get("characters")
print(systemGet)
print(gameGet)
print(charactersGet)
return redirect('temp.html')
return render_template_string(template, systems=systems)
template = """
<!doctype html>
<form method="POST">
<select id="system" name="system">
</select>
<select id="game" name="game">
</select>
<select id="characters" name="characters">
</select>
<button type="submit">Play</button>
</form>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
"use strict";
var systems = {{ systems|tojson }};
var form = $('form');
var system = $('select#system');
var game = $('select#game');
var characters = $('select#characters');
for (var key in systems) {
system.append($('<option/>', {'value': key, 'text': key}));
}
system.change(function(ev) {
game.empty();
game.append($('<option/>'));
var games = systems[system.val()];
for (var i in games) {
game.append($('<option/>', {'value': i, 'text': i}));
}
});
game.change(function(evV) {
characters.empty();
characters.append($('<option/>'));
var games = systems[system.val()][game.val()];
for (var i in games) {
characters.append($('<option/>', {'value': games[i], 'text': games[i]}));
}
});
</script>
"""
if __name__ == '__main__':
app.run()
