将Javascript变量传递给Python Flask [英] Passing Javascript Variable to Python Flask

查看:761
本文介绍了将Javascript变量传递给Python Flask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了不同示例中的几个帖子,通过post / get形式将JavaScript变量传递给flask。我仍然不明白如何做到这一点。根据我的理解,表单会创建一个post / get,然后可以由python flask脚本调用并接收。有人可以写一个非常简单的例子,看看应该是这样吗?



从创建一个带有javascript值的变量开始,然后进行post / get。最后,python的接收端应该是什么样子,最后从python打印出变量。

解决方案

来自JavaScript的ajax请求,看起来像这样。我认为最简单的方法也是使用JQuery,因为它可能会比纯JavaScript更冗长。

  //一些电影资料
var movies = {
'title':movie_title,
'release_date':movie_release_date
}

$ .ajax({
url:Flask.url_for('my_function'),
类型:'POST',
data:JSON.stringify(电影),//将js值转换为JSON字符串
})
.done(function(result){//成功获取服务器
的返回对象console.log(result)//对它做任何事情,在这种情况下,请在控制台
中看到它。

Flask.url需要JSGlue,基本上让你使用Flask的
url_for,但是使用javascript。它很容易安装和使用,否则我认为你可以用url替换它,比如'/ function_url'。然后在服务器端你可能会有这样的东西:

 来自瓶子的进口要求t,jsonify,render_template 
import sys

@ app.route(/ function_route,methods = [GET,POST])
def my_function()
if request.method ==POST:
data = {} //空字典存储数据
data ['title'] = request.json ['title']
data ['release_date'] = request.json ['movie_release_date']

//在数据库中做任何你想要的数据,比如在数据库或者其他东西中查找
//如果你想要打印到控制台

print(data,file = sys.stderr)

//然后返回成功返回前端

/ /这会返回收到的数据,您应该在浏览器控制台
//中看到它,因为脚本中的console.log()。
return jsonify(data)
else:
return render_template('the_page_i_was_on.html')

我认为要点是在jquery,flask的request.json()和jsonify()函数中查找ajax请求。


I have read several postings on different examples for passing a javascript variable to flask through post/get forms. I still don't understand how to do this. From my understanding, the form creates a post/get that can then be called and received by the python flask script. Can someone write up a very simple example on what this should look like?

Starting from creating a variable with any value in javascript and then making the post/get. Lastly what should the receiving end on python should look like and finally print the variable from python.

解决方案

How I did this was using an ajax request from the javascript which would look something like this. I think the easiest way would be using JQuery as well since it might be a bit more verbose with pure javascript.

// some movie data
var movies = {
    'title': movie_title,
    'release_date': movie_release_date
    }

$.ajax({
url: Flask.url_for('my_function'),
type: 'POST',
data: JSON.stringify(movies),   // converts js value to JSON string
})
.done(function(result){     // on success get the return object from server
    console.log(result)     // do whatever with it. In this case see it in console
}

Flask.url requires JSGlue which basically let's you use Flask's url_for but with javascript. Look it up, easy install and usage. Otherwise I think you could just replace it with the url e.g '/function_url'

Then on the server side you might have something like this:

from flask import request, jsonify, render_template
import sys

@app.route("/function_route", methods=["GET", "POST"])
def my_function():
    if request.method == "POST":
        data = {}    // empty dict to store data
        data['title'] = request.json['title']
        data['release_date'] = request.json['movie_release_date']

       // do whatever you want with the data here e.g look up in database or something
       // if you want to print to console

        print(data, file=sys.stderr)

        // then return something back to frontend on success

        // this returns back received data and you should see it in browser console
        // because of the console.log() in the script.
        return jsonify(data)
    else:
        return render_template('the_page_i_was_on.html')

I think the main points are to look up ajax requests in jquery, flask's request.json() and jsonify() functions.

这篇关于将Javascript变量传递给Python Flask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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