在html模板中使用Jinja2时出错:[UnicodeDecodeError]

时间:2019-04-18 19:04:29

标签: python html flask ascii jinja2

所以我有一个带有一些基本jinja表达式的html模板,例如: {{ post.content }} 我的html和python文件的编码为utf-8。 因此,这个jinja表达式正在加载一个我在名为db的python文件中创建的简单json,就像这样:

post = [{"content" : "Rémi"}]

为例。 问题是,当使用post.content=db.post渲染html模板时,出现此错误:

奇怪的是,几天前我没有收到此错误。

Traceback (most recent call last):
  File "/Library/Python/3.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Python/3.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/Library/Python/3.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/3.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Python/3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Python/3.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/routes.py", line 55, in feed
    return render_template('feed.html', posts=db.posts)
  File "/Library/Python/3.7/site-packages/flask/templating.py", line 135, in render_template
    context, ctx.app)
  File "/Library/Python/3.7/site-packages/flask/templating.py", line 117, in _render
    rv = template.render(context)
  File "/Library/Python/3.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/Library/Python/3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/feed.html", line 56, in top-level template code
    <p  id="content">{{ post.content }}</p>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)

我认为问题是json的编码,但我仍然是一个初学者,不知道如何更改它... 我会感谢您的帮助:)

0 个答案:

没有答案
相关问题