在Flask中,我如何在一个.py文件中编写条件并只在HTML中打印结果?

huangapple go评论69阅读模式
英文:

In Flask, how can I write a condition in a file py (not html) and print only result in html?

问题

I'm new to Flask. In the index.html page, there are a combobox and a textbox. If I select the Red item of the combobox, I would like the textbox to print "You have selected the item Red".

I would like to achieve this inside in the python file and not in the html file. I saw that conditions can also be written in the html file, but I don't want to write them there. So I would like to write the condition in the app.py python file and NOT in the index.html. Something like this: if colours.get() == "Red". Obviously, however, important, I would like to display the output in the textbox index.html.

How can I get this? Thank you all!

Flask Code: app.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route("/combobox", methods=["GET","POST"])
def dropdown():
    colours = ["Red", "Blue", "Black", "Orange"]
    return render_template("index.html", colours=colours)

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

Textbox and combobox code: index.html

<form name="Item_1" action="/getLigand" method="POST">
    <select name="colours">
        {% for colour in colours %}
        <option value="{{ colour }}" SELECTED>{{ colour }}</option>
        {% endfor %}     
    </select>
</form>

<form action="/result" method="POST">
    <input type="text" name="result" />
</form>

Please note that I've translated the provided text and code for you.

英文:

I'm new to Flask. In the index.html page, there are a combobox and a textbox. If I select the Red item of the combobox, I would like the textbox to print "You have selected the item Red".

在Flask中,我如何在一个.py文件中编写条件并只在HTML中打印结果?

I would like to achieve this inside in the python file and not in the html file. I saw that conditions can also be written in the html file, but I don't want to write them there. So I would like to write the condition in the app.py python file and NOT in the index.html. Something like this: if colours.get() == &quot;Red&quot;. Obviously, however, important, i would like to display the output in the textbox index.html

How can i get this? Thank you all!

Flask Code: app.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route(&#39;/&#39;)
def index():
    return render_template(&#39;index.html&#39;)

@app.route(&quot;/combobox&quot;, methods=[&quot;GET&quot;,&quot;POST&quot;])
def dropdown():
    colours = [&quot;Red&quot;, &quot;Blue&quot;, &quot;Black&quot;, &quot;Orange&quot;]
    return render_template(&quot;index.html&quot;, colours=colours)

if __name__ == &#39;__main__&#39;:
     app.run(host=&quot;0.0.0.0&quot;, port=8080)

Textbox and combobox code: index.html

  &lt;form name=&quot;Item_1&quot; action=&quot;/getLigand&quot; method=&quot;POST&quot;&gt;
    &lt;select name=&quot;colours&quot;&gt;
      {% for colour in colours %}
     &lt;option value=&quot;{{ colour }}&quot; SELECTED&gt;{{ colour }}&lt;/option&gt;
      {% endfor %}     
   &lt;/select&gt;
  &lt;/form&gt;
  
&lt;form action = &quot;/result&quot; method = &quot;POST&quot;&gt;
  &lt;input type=&quot;text&quot; name=&quot;result&quot; /&gt;
  &lt;/form&gt;

答案1

得分: 0

以下是已翻译好的内容:

这是一个可行的示例。这是app.py

from flask import Flask, render_template, request

app = Flask(__name__)

colours = ["红色", "蓝色", "黑色", "橙色"]

@app.route('/')
def index():
    return render_template("index.html", colours=colours, chosen="黑色")

@app.route("/combobox", methods=["GET","POST"])
def dropdown():
    picked = request.form['colours']
    if picked == '红色':
        message = "<<< 你选择了红色"
    elif picked == '蓝色':
        message = "<<< 你选择了蓝色"
    elif picked == '黑色':
        message = "<<< 你选择了黑色"
    else:
        message = "<<< 哦,不好,你选择了橙色"

    return render_template("index.html", colours=colours, chosen=picked, message=message)

if __name__ == '__main__':
     app.run(host="127.0.0.1", port=8080)

这是templates/index.html

<form name="Item_1" action="/combobox" method="POST">
    <select name="colours">
      {% for colour in colours %}
        {% if colour == chosen %}
         <option value="{{ colour }}" SELECTED>{{ colour }}</option>
        {% else %}
         <option value="{{ colour }}">{{ colour }}</option>
        {% endif %}     
      {% endfor %}     
   </select>
   <input type="submit">
</form>
  
<form action="/result" method="POST">
  <input type="text" name="result" value="{{ message }}" />
</form>

跟进:
现在,写了这么多,我开始思考你是否希望在他们选择颜色后立即显示该消息,而无需单击“提交”按钮。如果这是你想要的,那可以做到,但这是一种非常不同的技术。所有这些都必须在Javascript中完成,作为嵌入到HTML中的代码。这完全在浏览器内处理。它不会发送任何内容到Web服务器,因此你的Python代码不会参与其中。

英文:

Here is an example that works. This is app.py:

from flask import Flask, render_template, request

app = Flask(__name__)

colours = [&quot;Red&quot;, &quot;Blue&quot;, &quot;Black&quot;, &quot;Orange&quot;]

@app.route(&#39;/&#39;)
def index():
    return render_template(&quot;index.html&quot;, colours=colours, chosen=&quot;Black&quot;)

@app.route(&quot;/combobox&quot;, methods=[&quot;GET&quot;,&quot;POST&quot;])
def dropdown():
    picked = request.form[&#39;colours&#39;]
    if picked == &#39;Red&#39;:
        message = &quot;&lt;&lt;&lt; You chose red&quot;
    elif picked == &#39;Blue&#39;:
        message = &quot;&lt;&lt;&lt; You chose blue&quot;
    elif picked == &#39;Black&#39;:
        message = &quot;&lt;&lt;&lt; You chose black&quot;
    else:
        message = &quot;&lt;&lt;&lt; Oh no, you chose orange&quot;

    return render_template(&quot;index.html&quot;, colours=colours, chosen=picked, message=message)

if __name__ == &#39;__main__&#39;:
     app.run(host=&quot;127.0.0.1&quot;, port=8080)

This is templates/index.html:


&lt;form name=&quot;Item_1&quot; action=&quot;/combobox&quot; method=&quot;POST&quot;&gt;
    &lt;select name=&quot;colours&quot;&gt;
      {% for colour in colours %}
        {% if colour == chosen %}
         &lt;option value=&quot;{{ colour }}&quot; SELECTED&gt;{{ colour }}&lt;/option&gt;
        {% else %}
         &lt;option value=&quot;{{ colour }}&quot;&gt;{{ colour }}&lt;/option&gt;
        {% endif %}     
      {% endfor %}     
   &lt;/select&gt;
   &lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;
  
&lt;form action = &quot;/result&quot; method = &quot;POST&quot;&gt;
  &lt;input type=&quot;text&quot; name=&quot;result&quot; value=&quot;{{ message }}&quot; /&gt;
&lt;/form&gt;

Followup

Now, having written all that, I'm starting to wonder if you wanted that message to show up as soon as they PICK the color, without having to click a "Submit" button. If that's what you're after, that can be done, but it's a very different technique. That all has to be done in Javascript, as code that you embed within your HTML. That's all handled entirely within the browser. It doesn't send anything to the web server, so your Python code will not be involved.

huangapple
  • 本文由 发表于 2023年6月15日 15:09:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定