Python 如何获取烧瓶中的选择标记值

Python 如何获取烧瓶中的选择标记值,python,html,flask,Python,Html,Flask,我总共有两个python脚本。一个用于烧瓶本身,另一个用于后端计算。我有一个HTML文件 在backend.py中: def get_country(): county_name = ["Bangladesh", "India", "USA"] country_default = "Bangladesh" return country_name, country_default 在烧瓶_ap

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

在backend.py中:

def get_country():
    county_name = ["Bangladesh", "India", "USA"]
    country_default = "Bangladesh"
    return country_name, country_default
在烧瓶_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>

{国家/地区中的国家/地区为%}
{{国家}
{%endfor%}
挑选
您已选择{{default}}

我这里的问题是:

  • 如何使HTML文件中的select标记选择默认值 最初的价值
  • 如何在html文件中提交select标记值并更新
    country\u backend.py中的默认值
    变量

  • 您问题的答案:

    <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>
    
  • 您可以使用选项标记中的
    selected
    属性将第一个选项声明为默认值。然后,您应该从
    国家/地区名称中删除默认值
  • 您可以通过两种方式提交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>
    
    如果您使用GET方法,则它将使用查询参数将您重定向到“/”路线(选择国家)。路由可能如下所示,
    “/select\u country=(value\u selected)”
    。您可以使用
    request.args.get(query\u 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)
    
    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)
    
    如果使用POST方法,则不会更改路由。因此不会有任何查询参数。您应该改为使用
    requests.form.get(name)
    来获取发布的数据。您的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)
    
    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)
    

    关于后端关于HTML:谢谢。我已经看到了这个解决方案。它给出了在“flask.py”脚本中获取所选值的解决方案。但是,我需要在'backend.py'脚本中获取所选的值。谢谢。它正在工作。但是,我需要将所选的国家/地区值带回backend.py脚本进行一些额外的计算。另外,我必须保留默认的国家变量。知道我该怎么做吗?我对烧瓶不熟悉。试着通过做事来学习。如果您想在BeutEn.Py中保留默认的国家变量,请考虑使用一个数据库(MySQL、SQLite、……),否则每次关闭应用程序时,默认的国家将返回“孟加拉”。您应该修改backend.py。