如何在烧瓶中获取选择标签值

时间:2021-04-27 14:57:48

标签: python html flask

我总共有两个 python 脚本。一个用于烧瓶本身,另一个用于后端计算。我有一个 HTML 文件。

在 backend.py 中:

def get_country():
    county_name = ["Bangladesh", "India", "USA"]
    country_default = "Bangladesh"
    return country_name, country_default

在flask_app.py中:

import backend   
from flask import Flask
app = Flask(__name__) 
@app.route('/', methods=['GET', 'POST'])
    def home():
        country_name, country_default = backend.get_country()
        return render_template("index.html", country=county_name, default=country_default)

在 index.html 中:

<form action="" class="form-text" method="GET">
    <div class="row">
        <div class="col-10">
            <select name="select_country" class="form-select form-select-lg mb-3" aria-label=".form-select-lg example">
                {% for country in country %}
                <option value="{{country}}">{{country}}</option>
                {% endfor %}
            </select>
        </div>
        <div class="col-2">
            <button type="submit" class="btn btn-outline-primary">Select</button>
        </div>
    </div>
</form>
<p>You have selected {{default}}</p>

我在这里的问题是:

  1. 如何使 HTML 文件中的 select 标记选择 default 初始值?
  2. 如何在 html 文件中提交 select 标签值并更新 country_default 变量在 backend.py 中?

1 个答案:

答案 0 :(得分:0)

回答您的问题:

  1. 您可以使用选项标记中的 selected 属性将第一个选项声明为默认值。然后,您应该从 country_name 中删除默认值。
  2. 您可以通过两种方式提交 select 标签,使用 GET 方法或 POST 方法。

您的 index.html 应如下所示:

<form action="/" class="form-text" method="GET/POST (Choose One)">
    <div class="row">
        <div class="col-10">
            <select name="select_country" class="form-select form-select-lg mb-3" aria-label=".form-select-lg example">
                <option value="{{default}}" selected>{{default}}</option>
                {% for country in country %}
                <option value="{{country}}">{{country}}</option>
                {% endfor %}
            </select>
        </div>
        <div class="col-2">
            <button type="submit" class="btn btn-outline-primary">Select</button>
        </div>
    </div>
</form>
<p>You have selected {{default}}</p>

您的 backend.py 应如下所示:

def get_country(default):
    county_name = ["Bangladesh", "India", "USA"]
    country_default = "Bangladesh"
    if default in country_name:
        country_default = default
    country_name.remove(country_default)
    return country_name, country_default

如果您使用 GET 方法,那么它会将您重定向到带有查询参数 (select_country) 的“/”路由。路线可能如下所示,"/select_country=(value_selected)"。您可以使用 request.args.get(query_name) 在 Flask 中获取查询参数。您的flask_app.py 应如下所示:

from backend import get_country
from flask import Flask, render_template
app = Flask(__name__) 

@app.route('/')
def home():
    country_name, country_default = get_country(request.args.get("select_country"))
    return render_template("index.html", country=county_name, default=country_default)

如果你使用POST方法,那么它不会改变路由。因此不会有任何查询参数。您应该改为使用 requests.form.get(name) 来获取发布的数据。您的flask_app.py 应如下所示:

from backend import get_country
from flask import Flask, render_template
app = Flask(__name__) 

@app.route('/', methods=['GET', 'POST'])
def home():
    country_name, country_default = get_country(request.form.get("select_country"))
    return render_template("index.html", country=county_name, default=country_default)