javascript - 从 javascript 将参数传递给 Flask

标签 javascript python flask jinja2

当按下按钮时,我在 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/

相关文章:

JavaScript 相当于 Java 的 URLEncoder.encode ("String", "UTF-8")

javascript - javascript中的原型(prototype)继承,它是如何工作的?

python - 如何在 Microsoft Azure Notebook 中使用 display() 函数

Python 实例变量显然共享数据

python - 从字体创建文本预览的方法

python - Heroku Python仅处理第一个异常

javascript - 让 UploadFS 与 angular2-meteor 一起工作

javascript - 在 Dynamics 365 中,是否有一种方法可以根据记录的字段值和用户权限筛选关联的 View 结果?

python - 如何从Python请求中检索字典数据?

python - url_for 迁移到 Flask Restful