JSON is appearing as unicode entities in Jinja2 template

前端 未结 3 1188
傲寒
傲寒 2021-01-04 06:20

I using Jinja2 with webapp2.

Jinja2 encodes all \'context\' data into unicode as their doc says. This is proving problematic when I try to insert a json string into

3条回答
  •  执念已碎
    2021-01-04 07:01

    You must filter the value through the safe filter to tell jinja2 that it shouldn't apply any other filters to the output. In jinja2 syntax this would be:

    {{ jsonData | safe }}
    

    Note that since you are calling json.loads you actually do not have json data anymore, you have a python list object. Thus when it is serialized it's the same as calling unicode(['dogs', 'cats']) which is going to give you your u prefix. You might not want to actually parse the json data, or you'll need to turn the list into a string manually instead of having jinja2 do it for you.

提交回复
热议问题