如何访问选定的下拉列表回到烧瓶 [英] how to access the selected dropdown back to flask

查看:67
本文介绍了如何访问选定的下拉列表回到烧瓶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试通过单击按钮来将选定的下拉列表返回到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',methods=['GET','POST'])
    def connect_management():
       user = (request.form['selected_class']).first()
       return (str(user))

app.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.

I need to use the selected dropdown option and populate few more results from Flask api on a click of button and display them in countdown tag below the button. To sum up I need to access the dropdown value back to the Flask api without new tab.

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

我一直在获得

TypeError: 'ImmutableMultiDict' object is not callable

即使遵循了建议

推荐答案

我无法理解您的代码错误.可能是因为不同的代码而导致错误,但没有显示完整的错误消息来确认它.

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.

要在不重新加载页面的情况下更新HTML,您需要JavaScriptjQuery来发送AJAX请求,获取响应并将其放入现有的HTML

To update HTML without reloading page you need JavaScript or jQuery to send AJAX request, get response and put it in existing HTML

最小工作示例:

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()


顺便说一句:在从HTML进行烧瓶变量访问中继续


BTW: Continuation in flask variable access from HTML

这篇关于如何访问选定的下拉列表回到烧瓶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆