Flask中的select和request.form.get和getlist出现问题

2024-10-02 14:18:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题没那么严重,只是有点烦人。我有一个下拉菜单和一个值列表;但是,我的值将自己重置为第一个选项,我希望它们保留为用户选择的值

我从其他来源了解到,解决方案使用的是getlist而不是get,但当我尝试这样做时,出现以下错误:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

我几乎没有使用flask和Jinga的经验。我想这一定是值类型的问题,或者是我需要获取的某种类型的名称或值的问题。。。我真的不知道。任何帮助都将不胜感激

下面是flask如何处理request.form.get的video,下面是我为特定html视图和我请求数据的应用程序片段编写的代码

@app.route('/apuntual', methods = ['GET','POST'])
def apunt():
    if request.method == 'POST':
        # This is the button that I click on the video
        if request.form.get('capturar') == 'capturar':
            sample_rate_unf = request.form.get('f_muestreo')
            samples_to_acq_unf = request.form.get('m_canal')
            #Changing the values to int so I can work with them
            sample_rate = int(sample_rate_unf)
            samples_to_acq = int(samples_to_acq_unf)
            #lots of more code in here
            #
            # Sending the output values to make the graph, and some other values to 
            # display in the html
            return render_template('apuntual.html', fmax = fmax, tad = tad, imagen = datos)
<div class="col-md-2">
            
    <form  method="POST">
            
        <h5 class="mb-1">Frecuencia de muestreo</h5>        
        <select name="f_muestreo">
            <option value="2048">2048</option>
            <option value="2560">2560</option>
            <option value="3200">3200</option>
            <option value="5120">5120</option>
        </select>
                
        <h5 class="mb-1">Muestras por canal</h5>
        <select name="m_canal">
            <option value="2048">2048</option>
            <option value="4096">4096</option>
            <option value="8192">8192</option>
        </select>
                    
                
        <h5 class="mb-1">Captura instantánea</h5>
        <p class="bs-component">    
            <input type="submit" class="btn btn-primary" name="capturar" value="capturar">
            <input type="submit" class="btn btn-primary" name="borrar" value="borrar">
        </p>
                
        <p class=""bs-component>Frecuencia máxima de: {{ fmax }} Hz con TAD: {{ tad }} ms.</p>  
                    
    </form>
</div>

Tags: thetonameformgetvaluerequestselect
1条回答
网友
1楼 · 发布于 2024-10-02 14:18:32

我能想到的一个解决方案是将所选选项作为变量传递给模板,并在模板中标记所选选项。下面是一个演示:

@app.route("/", methods=("GET", "POST"))
def demo():
    options = (1, 2, 3, 4)
    selected = None
    
    if request.method == "POST":
        selected = int(request.form.get("something"))
        # Rest of the code goes here

    return render_template("demo.html", options=options, selected=selected)
<form method="POST">
    <select name="something">
        {% for op in options %}
            {% if selected == op %}
                <option value="{{ op }}" selected>{{ op }}</option>
            {% else %}
                <option value="{{ op }}">{{ op }}</option>
            {% endif %}
        {% endfor $}
    </select>
    <input type="submit">
</form>

请注意,我将所有选项作为元组放在服务器代码中。其中一个原因是避免在模板代码中重复。通常认为,将数据直接存储到前端代码是一种不好的做法,就像您在这里所做的那样。我的演示也不完美。更好的解决方案是将所有这些选项放入配置文件中

相关问题 更多 >