从 javascript 将参数传递给 Flask

Bon*_*bin 5 javascript python jinja2 flask

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

JavaScript:

<script>
    function ToPython(FreeSearch,LimitContent)
    {
        alert(FreeSearch);
        alert(LimitContent);
    }
</script>
Run Code Online (Sandbox Code Playgroud)

我想调用的烧瓶函数:

@app.route('/list')
def alist(FreeSearch,LimitContent):
    new = FreeSearch+LimitContent;
    return render_template('list.html', title="Projects - " + page_name, new = new)
Run Code Online (Sandbox Code Playgroud)

我想做一些类似于"filename.py".alist(FreeSearch,LimitContent)JavaScript 的事情,但这是不可能的......

Jér*_*ôme 3

从 JS 代码中,调用(使用 GET 方法)烧瓶路由的 URL,将参数作为查询参数传递:

/list?freesearch=value1&limit_content=value2
Run Code Online (Sandbox Code Playgroud)

然后在你的函数定义中:

@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)
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用路径变量:

/list/value1/value2
Run Code Online (Sandbox Code Playgroud)

@app.route('/list/<freesearch>/<limit_content>')
def alist():
    new = free_search + limit_content
    return render_template('list.html', title="Projects - "+page_name, new=new)
Run Code Online (Sandbox Code Playgroud)