无法使用Flask从下拉列表中在python中选择值

无法使用Flask从下拉列表中在python中选择值

本文介绍了无法使用Flask从下拉列表中在python中选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能是重复的,但我已经检查了所有此类相关问题的答案,但无法解决.

This question could be a duplicate but I have checked all the answers of such related questions and I haven't been able to solve it.

我正在尝试从包含数字的下拉菜单中获取值.然后,我想将数字与一个值进行比较,并根据比较结果显示文本.

I am trying to get the value from a dropdown menu which consists of numbers. Then I want to compare the numbers with a value and display a text based on the comparison.

例如

if value_selected_from_dropdown >3
 display text

我无法显示文本,甚至无法打印所选选项的值.

I am unable to get the text to display or even print the value of the option selected.

这是python文件web_plants.py

Here is the python file, web_plants.py

from flask import Flask, render_template,request, redirect, url_for

app = Flask(__name__)

def template(title = "HELLO!", text = ""):
    templateDate = {
        'text' : text
        }
    return templateDate

@app.route("/threshold", methods=['POST'])
def threshold():
        tvalue= (request.form.get['tvalue']) #get value from dropdown
        msg= ""
        if tvalue>3:
          msg= "rating above 3"
         templateData = template(text = msg) #display text using template()
        #templateData = template(text = tvalue) #tried to print the value selected
        return render_template('index.html', **templateData)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80, debug=True)

index.html:

index.html:

<!DOCTYPE HTML>
<html>

<head>
 <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='style.css')}}" />
</head>

<body>

 <h2> {{ text }} </h2>
<form action= "{{ url_for('threshold') }}" method="post>"
             <p>
             <select name= 'tvalue'>
                 <option value="10">10</option>
                 <option value="11">11</option>
                 <option value="15">15</option>
                 <option value="2">2</option>
                 <option value="1">1</option>
              </select>
              </p>
    </form>
 </body>
</html>

推荐答案

有几种方法可以实现此目的.您可以为模板本身提供逻辑,也可以在function threshold中添加逻辑.

There are several ways to achieve this. Either you can give logic to the template itself or you can add the logic in the function threshold.

index.html

<h2> {{text}} </h2>
<form action= "{{ url_for('threshold') }}" method="POST">

     <select name= 'tvalue'>
      {% for tvalue in tvalues %}
        {% if selected_tvalue == tvalue %}
            <option value="{{ tvalue }}" selected='selected'>{{ tvalue }}</option>
        {% else %}
             <option value="{{ tvalue }}" >{{ tvalue }}</option>
        {% endif %}
      {% endfor %}

      </select>

     <input type="submit" value="Submit" />
</form>

{% if selected_tvalue > 3 %}
    <h2> Selected value is greater than 3 </h2>
{% else %}
     <h2> Selected value is less than or equal to 3 </h2>
{% endif %}
<form action= "{{ url_for('threshold') }}" method="POST">

     <select name= 'tvalue'>
      {% for tvalue in tvalues %}
        {% if selected_tvalue == tvalue %}
            <option value="{{ tvalue }}" selected='selected'>{{ tvalue }}</option>
        {% else %}
             <option value="{{ tvalue }}" >{{ tvalue }}</option>
        {% endif %}
      {% endfor %}

      </select>
    <input type="submit" value="Submit" />
</form>

server.py

def template(title = "HELLO!", text = ""):
    templateDate = {
        'text' : text,
        'tvalues' : getTValues(),
        'selected_tvalue' : -1
    }
    return templateDate

def getTValues():
    return (10, 11, 15, 2, 1)

@app.route("/threshold", methods=['POST', 'GET'])
def threshold():
    tvalue= -1 #default value
    msg = ''
    if request.method == "POST":
        tvalue = int(request.form['tvalue'])
        if tvalue> 3:
            msg= "rating above 3"

    #generating template data
    templateData = template(text = msg)
    templateData['selected_tvalue'] = tvalue

    return render_template('index.html', **templateData)

然后在路径/threshold中访问您的表单.希望对您有帮助.

Then access your form at path /threshold. I hope it helps.

这篇关于无法使用Flask从下拉列表中在python中选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 01:29