0

I make some simple web with flask (for educational purpose). when I try to assign a string value inside a python dict, all apostrophe(') change into '. here is my dummy code:

@app.route('/')
def index():
    a={}
    dummy=[]
    dummy2={}
    dummy2['SGD']='testing'
    dummy2['USD']='testingagain'
    dummy.append(dummy2)
    a['lalala']=dummy
    dummy=[]
    dummy3={}
    dummy3['SGD']='testtillbored'
    dummy3['USD']='testtillbored2'
    dummy.append(dummy3)
    a['lalalala']=dummy
    return render_template('main.html',**locals())

and my main.html:

<!DOCTYPE HTML>
<html>
<meta charset='utf-8'>
<body>
<script>
var x={{a}};
console.log(x);
</script>
</body></html>

Actually, I have succeeded in getting data when I using Ajax call. So I dump the dict into json and catch it when the webpage loads. So my question is, how to get the data in json without having to use ajax call?

0

1 Answer 1

0

You can use the jinja json filter

<script>
var some_json = {{some_dict|tojson|safe}}
console.log(some_json)
</script>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.