猿问

如何访问选定的下拉菜单回烧瓶

我一直在尝试通过Flask单击按钮访问选定的下拉列表。基于我做了如下的建议之一


应用程序.py


    @app.route('/ra/connect',methods=['GET','POST'])

    def connect_management():

       user = (request.form['selected_class']).first()

       return (str(user))

应用程序.html


              <select name="selected_class" class="form-control" id="all_classes">


                  {% for o in all_classes %}

                  <option  value="{{ o }}" selected>{{ o }}</option>

                  {% endfor %}   

              </select> 

我需要使用选定的下拉选项并Flask通过单击按钮从 api 填充更多结果并将它们显示在countdown按钮下方的标记中。总而言之,我需要在Flask没有新选项卡的情况下将下拉值访问回 api。


<button class="form-control" id="button" onclick="connect4()">Get gateways</button>

                <p id="countdown"></p>

我一直在


TypeError: 'ImmutableMultiDict' object is not callable

即使在遵循建议之后


阿晨1998
浏览 156回答 1
1回答

一只甜甜圈

我无法通过您的代码得到您的错误。可能您收到不同代码的错误,但您没有显示可以确认它的完整错误消息。要在HTML不重新加载页面的情况下更新您需要JavaScript或jQuery发送AJAX请求,获取响应并将其放入现有HTML最小的工作示例:from flask import Flask, request, render_template_stringapp = Flask(__name__)@app.route('/')def index():&nbsp; &nbsp; 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">&nbsp; &nbsp;<select name="selected_class" class="form-control" id="all_classes">&nbsp; &nbsp;{% for o in all_classes %}&nbsp; &nbsp; &nbsp; <option&nbsp; value="{{ o }}" selected>{{ o }}</option>&nbsp; &nbsp;{% endfor %}&nbsp; &nbsp;&nbsp; &nbsp;</select>&nbsp;&nbsp; &nbsp;<button class="form-control" id="button">Get gateways</button></form><p id="countdown"></p><script>&nbsp; &nbsp; $('#button').click( function(event) {&nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; $.post("/ra/connect", $('#form').serialize(), function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //alert(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countdown = $("#countdown");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countdown.append(data + "<br/>");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });</script></html>''', all_classes=['Hello', 'World'])@app.route('/ra/connect', methods=['GET', 'POST'])def connect_management():&nbsp; &nbsp; user = request.form.get('selected_class')&nbsp; &nbsp; print('user:', user)&nbsp; &nbsp; return str(user)app.run()
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答