从javascript传递参数到烧瓶

时间:2016-10-12 10:17:35

标签: javascript python flask jinja2

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

javascript:

<script>
    function ToPython(FreeSearch,LimitContent)
    {
        alert(FreeSearch);
        alert(LimitContent);
    }
</script>

我想打电话的烧瓶功能:

@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)的操作但不可能......

1 个答案:

答案 0 :(得分:1)

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

@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)

{{1}}
相关问题