当按下按钮时,我在 html 文件中调用一个 JavaScript 函数,该函数接受两个字符串作为参数(来自输入字段)。当调用该函数时,我想将这些参数传递到我的 flask 文件并在那里调用另一个函数。我该如何实现这个目标?
JavaScript:
<script>
function ToPython(FreeSearch,LimitContent)
{
alert(FreeSearch);
alert(LimitContent);
}
</script>
我要调用的flask函数:
@app.route('/list')
def alist(FreeSearch,LimitContent):
new = FreeSearch+LimitContent;
return render_template('list.html', title="Projects - " + page_name, new = new)
我想在 JavaScript 中执行类似 "filename.py".alist(FreeSearch,LimitContent)
的操作,但这是不可能的...
最佳答案
从 JS 代码中,调用(使用 GET 方法) flask 路由的 URL,将参数作为查询参数传递:
/list?freesearch=value1&limit_content=value2
然后在你的函数定义中:
@app.route('/list')
def alist():
freesearch = request.args.get('freesearch')
limitcontent = request.args.get('limit_content')
new = freesearch + limitcontent
return render_template('list.html', title="Projects - "+page_name, new=new)
或者,您可以使用路径变量:
/list/value1/value2
和
@app.route('/list/<freesearch>/<limit_content>')
def alist():
new = free_search + limit_content
return render_template('list.html', title="Projects - "+page_name, new=new)
关于javascript - 从 javascript 将参数传递给 Flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39996133/