簡體   English   中英

Flask:將最后選擇的項目保留在下拉列表中(Jinja)

[英]Flask: Keep last selected item in drop downs (Jinja)

我從篩選器功能中選擇了一個國家,再次將此表格發送到篩選器功能截圖問題是,我的表格沒有在我的表格中保留最后選擇的國家(變量“國家”已發送)。 我總是在表單中有值“Any”

我的選擇示例列表 i id=1 => 'France' id=2 => 'Japan'... 所以如果選擇日本,(id=2) [2] 我想看日本而不是“任何”表格 [在此處輸入圖片描述]

你的幫助將不勝感激 謝謝

篩選器.html

<form method=POST action="{{ url_for('screener') }}" onchange=submit()>
  <table class="table table-sm table-hover">
    <thead>
    <tr><td>control : {{ country }}</td></tr>
    <tr>
        <td>Country</td>
        <td>
          <select id="country" name="country" class="form-select form-select-sm" aria-label="">
            <option value="">Any</option>
            {% for db_country in db_countries %}
              <option value="{{ db_country.id }}" {% if country == db_country.id %} selected {% endif %} >
                {{ db_country.id }} - {{ db_country.name }}`</option>
            {% endfor %}
          </select>
        </td>
    </tr>
    <tr>
  </table>
</form>

應用程序.py

`@app.route('/screener/', methods=['POST', 'GET'])
def screener():
    db_countries = Country.query.order_by(Country.name.asc()).all()
    if request.method == 'POST':
        country = request.form['country']
    else:
        country = 0
    return render_template('screener.html', title='Screener',db_countries=db_countries, country=country)`

您正在將strint進行比較。 因此,比較失敗。
將變量country更改為int並且它有效。

您可以更改端點中的類型

country = request.form.get('country', 0, type=int)

或在模板中。

{% if country | int == db_country.id %} selected {% endif %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM