html下拉菜单重新加载时,选择试图协助请求

2024-05-16 10:42:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个国家列表的下拉菜单,我最终将使用两次选择两个国家从数据库和图表中提取数据。使用html、jinja、python和flask

我可以从python中的SQL生成国家列表。。。我可以使用创建下拉菜单的jinja生成html模板。到目前为止还不错

但是当我单击下拉列表时,列表会短暂出现,但是列表会立即重新加载,因此实际上无法进行选择并移动到results.html

假设卡在循环中-可能是由于路线中的“/”和“/search”混淆?或者获取/发布问题?布局有问题吗

也似乎是一个国家之间的空白行,这将是很好的避免

非常感谢您的建议,请参阅extract from search.html和extract from application.py可以发布更多信息

@app.route("/", methods=["GET", "POST"])
def search():
"""Choose country"""
if request.method == "POST":
    # Ensure  Country A was submitted
    if not request.form.get("ctrya"):
        return apology("must select a country for Country A", 403)
    else:
        return render_template("results.html")
else:
    ctry_list = []
    # creating drop down list for the webpage
    countries = db.execute("SELECT country  FROM indicators GROUP BY country")
    for ctry in countries:
        ctry_list.append(ctry["country"])

    return render_template("search.html", country_list = ctry_list)    

对于html

{% extends "layout.html" %}
{% block title %}
Search
{% endblock %} 
{% block main %}
<form action="/" method="post">
  <div class="form-group">
      <select class="form-control" name="ctrya">
          <option disabled="" selected="" value="">Country A</option>
              {% for ctrya in country_list %}
                 <option value="{{ctrya["country"]}}">{{ctrya["country"]}}
                 </option>
                <option>{{ctrya}}</option>
              {% endfor %}
       </select>
    </div>

<button class="btn btn-primary" type="submit">Compare countries</button>
</form>
{% endblock %}

Tags: form列表forsearchreturnhtml国家select