简体   繁体   English

烧瓶传递json和render_template

[英]flask passing json and render_template

i'm a new dev , I've a Json object with data and i'm trying to pass it to a certain page using render_template 我是一个新开发人员,我有一个带有数据的Json对象,我正在尝试使用render_template将其传递到某个页面

I've seen couple of answers about this but for some reason none of them have worked for me 我已经看到了一些答案,但是由于某种原因,它们都没有为我工作

i know the connection between Javascript and Flask is solid as i can console.log("string") at Onload function in JS but it doesn't seem to recognise anything i pass with the render template 我知道Javascript和Flask之间的连接是牢固的,因为我可以在JS的Onload函数中使用console.log(“ string”),但是它似乎无法识别我通过渲染模板传递的任何内容

things i have tried so far 到目前为止我尝试过的事情

saving json object as variable and pass it with the render template. 将json对象保存为变量,并将其与渲染模板一起传递。

    x = {"Success": True , "Msg":" lets try to get jinja to work - email {{ current_user.email }} " , "displayname":current_user.name}
    t = "test"
    return render_template("index.html", x=x ,t=t)

on JS 在JS上

document.addEventListener('DOMContentLoaded' , function () {
console.log("loaded");


const onStay = new XMLHttpRequest();
onStay.open('post' , "stay" , true);
onStay.send(null);

onStay.onload = ()=>{

console.log("i got response from flask ... onstay.onload , cheers !");
//console.log(x);
var  rt = t;
console.log(rt);
var resStay = JSON.parse(x.responseText);
var tt = JSON.parse('{{ x | tojson | safe}}');

so i'm also testing if i can pass jinja2 code or flask_login code actually save it in a variable of some sort and respond to it with JS 所以我也在测试是否可以传递jinja2代码或flask_login代码,将其实际保存在某种变量中,并使用JS对其进行响应

the second attempt was trying to jsonify method and parse it 第二次尝试是尝试对方法进行jsonify解析

on python : 在python上:

X = jsonify({"Success": True , "Msg":" lets try to get jinja to work - 
email {{ current_user.email }} " , "displayname":current_user.name})
return render_template("index.html", X=X)

on JS end : 在JS端:

 onStay.onload = ()=>{
 var res = JSON.parse(reqSignup.responseText);
 console.log("res  === " , "\n" , res );

the output i get is 我得到的输出是

JavaScript.js:11 i got response from flask ... onstay.onload , cheers !
JavaScript.js:13 Uncaught ReferenceError: t is not defined
    at XMLHttpRequest.onStay.onload (JavaScript.js:13)
onStay.onload @ JavaScript.js:13
load (async)


JavaScript.js:191 qwe@qwe.qwe 
 qwe
JavaScript.js:313 DATA   =  FormData {}

The problem is that you are trying to access variables directly from JavaScript. 问题是您试图直接从JavaScript访问变量。 What you need to do is to use these variables in the Flask Template using Jinja and in it, put a call to your JavaScript. 您需要做的是使用Jinja在Flask模板中使用这些变量,然后在其中调用您的JavaScript。

A classic example is putting a button, with an onclick=YourFunction(x). 一个经典的示例是放置一个按钮,并带有onclick = YourFunction(x)。 When calling the function it will pass the value it has in the variable. 调用函数时,它将传递变量中具有的值。

You should be using the default Flask template engine which is Jinja , but if you prefer a template engine I developed to facilitate this kind of thing, if Sucuri . 您应该使用默认的Flask模板引擎Jinja ,但是如果您更喜欢我为方便起见而开发的模板引擎, 例如Sucuri

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM