将参数从 javascript 传递给烧瓶

Pass argument to flask from javascript

按下按钮时,我在我的 html 文件中调用一个 javascript 函数,该函数将两个字符串作为参数(来自输入字段)。当调用该函数时,我想将这些参数传递给我的烧瓶文件并在那里调用另一个函数。我将如何做到这一点?

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 方法)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)