Django文本输入导致呈现temp时出现wierd字符

2024-10-02 18:21:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道到底要搜索什么,所以我希望社区能为我指明正确的方向。在

在我的网站上,我有一个文本输入。 在其中一个输入中,有人复制并粘贴了文本

Vancouver marketing extraordinaire Bob Rennie asks “What if it’s not a bubble?

从这个链接 http://blog.buzzbuzzhome.com/2012/05/vancouver-marketing-bob-rennie-bubble.html 你会注意到引号和撇号有点不同。在

对象保存良好。在

当模板呈现输出该字段时,问题就出现了。我得到了:

Vancouver marketing extraordinaire Bob Rennie asks 1CWhat if it 19s not a bubble?

(注意1C19s其中引号和撇号应该在这里)

我以为这可能是一个utf-8问题,但我已经在我的html文件的顶部

<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

Tags: 文本httpifhtmlnotitmarketing引号
1条回答
网友
1楼 · 发布于 2024-10-02 18:21:17

我认为在返回响应之前,应该先对unicode字符串进行编码。在

In [17]: from django.template import Template, Context                                                                                                                   

In [18]: t = Template("{{ text }}")

In [19]: c = Context({'text': 'Vancouver marketing extraordinaire Bob Rennie asks “What if it’s not a bubble?'})                                                         

In [20]: print t.render(c)
Vancouver marketing extraordinaire Bob Rennie asks “What if it’s not a bubble?

In [21]: t.render(c)
Out[21]: u'Vancouver marketing extraordinaire Bob Rennie asks \u201cWhat if it\u2019s not a bubble?'

当我将t.render(c).encode('utf-8')写入html文件并添加其他元信息时,它显示enter image description here

相关问题 更多 >