将参数从javascript传递到flask [英] Pass argument to flask from javascript

查看:659
本文介绍了将参数从javascript传递到flask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下按钮时,我在html文件中调用了一个javascript函数,该函数以两个字符串作为参数(来自输入字段).调用该函数时,我想将这些参数传递到我的flask文件并在那里调用另一个函数.我该怎么办?

When pressing a button i call a javascript function in my html file which takes two strings as parameters (from input fields). When the function is called i want to pass these parameters to my flask file and call another function there. How would i accomplish this?

javascript:

The javascript:

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

我要调用的flask函数:

The flask function that i want to call:

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

I want to do something like "filename.py".alist(FreeSearch,LimitContent) in the javascript but its not possible...

推荐答案

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

From JS code, call (using GET method) the URL to your flask route, passing parameters as query args:

/list?freesearch=value1&limit_content=value2

然后在您的函数定义中:

Then in your function definition:

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

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

Alternatively, you could use path variables:

/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传递到flask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆