【发布时间】:2021-07-20 06:26:14
【问题描述】:
我有一个 html 搜索框。 如果用户敲击键盘,我想将这个尚未提交的变量传递给 python 烧瓶。 我不知道如何处理这个。 第一个代码是html搜索框部分,第二个代码是python烧瓶代码。
<br> <input name="Country" type="text" id="Country" placeholder="Enter Country" oninput="myFunction()"/> oninput: <span id="result"></span>
<script>
function myFunction() {
// result.innerHTML = Country.value;
result.innerHTML = output;
};
</script>
@auth.route('/search', methods=['POST'])
def autoComplete():
country = request.form[Country.value]
output = autoCompletion.init(country)
return render_template("search.html", output=output)
有没有办法将尚未提交的html搜索框字符串传递给python烧瓶? 任何帮助将不胜感激。
【问题讨论】:
-
您是否使用 python flask 托管网页?如果您要托管网页,则需要从您的文档向该网页发出 POST 请求。
-
是的,我是。您在这里看到任何问题 country = request.form[Country.value] 吗?
-
可以通过字段名称请求表单字段的数据。所以表达式是
country = request.form['Country']。但是,我认为您想使用 Ajax GET 请求来获取自动完成的数据。因此,页面不会重新加载,而是异步传输数据,然后将其集成到页面中。 -
country = request.form['Country'] 我以为这个只在用户提交时使用。
-
我试图在用户提交之前传递变量