简体   繁体   中英

How to render special characters in jinja templates with flask

I am fetching data from an API as JSON in Flask, and attempting to render this out to a Jinja Template as RSS, but characters from the JSON such as apostrophes and emdashes get rendered out as ’ and — in the template.

How do I ensure the template renders the proper characters?

This is my code in flask:

json_resp = resp.json()
posts = json_resp['list']
template = render_template('recs.rss',posts=posts)
response = make_response(template)
response.headers.set('Content-Type', 'application/rss+xml')
return response

and this is the recs.rss template:

<?xml version="1.0"?>
<rss version="2.0">
   <channel>
      ...
      {% for post in posts %}
      <item>
        {% if post.title %}<title><![CDATA[{{ post.title }}]]></title>{% endif %}
        ...
      </item>
      {% endfor %}
   </channel>
</rss>

If I encode it as utf-8:

post_item['title'] = post_item['title'].encode('utf-8'

I get utf-8 encoding in the RSS feed, and it seems to be prefaced with a b instead of a u :

b'The Dirty Secret of \xe2\x80\x98Secret Family Recipes\xe2\x80\x99'

If I attempt to then decode using utf-8 as suggested in the comment below and this post:

Python Selenium ().text returns "’" instead of apostrophe (')

I get an error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x91 in position 20: invalid start byte

I'm wondering if there is some way to fix this with headers that I send to the jinja template, or something that can be placed within the Jinja template itself to allow for the existance of things like curly quotes.

You should specify the response body encoding in the Content-Type header with application/rss+xml; charset=utf-8 application/rss+xml; charset=utf-8 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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