如果您希望客户端动态处理某些内容,则无法编写一些Javascript。幸运的是,这不是您估计的400多条线。此示例未使用WTForms,但这并不是很重要。关键部分是将可用选项作为JSON发送,并动态更改可用选项。这是一个可运行的单文件Flask应用程序,演示了基本思想。
from flask import Flask, render_template_stringapp = Flask(__name__)@app.route('/')def index(): systems = { 'PlayStation': ['Spyro', 'Crash', 'Ico'], 'N64': ['Mario', 'Superman'] } return render_template_string(template, systems=systems)template = """<!doctype html><form> <select id="system"> <option></option> </select> <select id="game"></select> <button type="submit">Play</button></form><script src="//pre.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'); 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': games[i], 'text': games[i]})); } }); form.submit(function(ev) { ev.preventDefault(); alert("playing " + game.val() + " on " + system.val()); });</script>"""app.run()


