Django 模板:将 unicode 转换为 utf-8——一个非常常见的非内置操作
这是关于 django 模板和 unicode 值的基本问题。
一个常见的用例是将 unicode 值传递给 django 模板,但这些值无法打印出来!
用户对 django 表单的输入值被编码为 unicode: https://docs.djangoproject.com/en/dev/ref/ unicode/#form-submission
因此,视图通常使用 unicode 值。 当需要输出这些值时,需要将它们编码为 utf-8。 我想在 django 模板中进行编码,但似乎没有内置的过滤器。例如:这篇文章描述了创建自定义过滤器: http://seewah.blogspot.com/2009/11/django -template-urlencode-unicode.html
即使jinja2也不提供这种类型的内置过滤器。
因此,虽然我可以将一个对象传递到我的模板并访问该对象内的各种结构,但它并没有那么有用,因为我无法将这些结构中的 unicode 字符串转换为 utf-8 字符串!
我在这里错过了什么吗?
更新(1小时后):
让我在这里放一些伪代码以使其更清楚:
在 django 模板中,我有类似的内容:
{% for an_obj in list_of_obj %}
<li><a href="/my_url/?send_string={{an_obj.a_unicode_field | urlencode}}">{{an_obj.a_unicode_field}}</a></li>
{% endfor %}
但是当 a_unicode_field 是 unicode 值时,这将失败。我想做的是:
<li><a href="/my_url/?send_string={{an_obj.a_unicode_field | encode: "utf-8" | urlencode}}">{{an_obj.a_unicode_field | encode: "utf-8"}}</a></li>
但是,没有内置的“编码”过滤器。这是一个非常常见的操作:我需要对每个模板中输出的每个字符串进行 utf-8 编码......
Here's a basic question about django templates and unicode values.
A common use case is unicode values passed to django templates, and yet these values can't be printed out!
The user's input values to django forms are encoded as unicode:
https://docs.djangoproject.com/en/dev/ref/unicode/#form-submission
Thus, views generally work with unicode values.
When it's time to output those values, they need to be encoded as utf-8.
I'd like to do that encoding in the django template, but there does not seem to be a built-in filter for that. eg: this post describes creating a custom filter:
http://seewah.blogspot.com/2009/11/django-template-urlencode-unicode.html
Even jinja2 does not provide this type of built-in filter.
So, although I can pass in an object to my template and access various structures inside that object, it's not that useful because i can't convert unicode strings in those structures to utf-8 strings!
Am I missing something here?
Update (1 hour later):
Let me put some pseudo-code here to be more clear:
In a django template I have something like:
{% for an_obj in list_of_obj %}
<li><a href="/my_url/?send_string={{an_obj.a_unicode_field | urlencode}}">{{an_obj.a_unicode_field}}</a></li>
{% endfor %}
But this will fail when a_unicode_field is a unicode value. What I want to do is:
<li><a href="/my_url/?send_string={{an_obj.a_unicode_field | encode: "utf-8" | urlencode}}">{{an_obj.a_unicode_field | encode: "utf-8"}}</a></li>
But, there is no built-in "encode" filter. And it's a very common operation: I need to do this utf-8 encoding for every string I output in every template...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您看到:
[u'keyword'],例如写入模板:{{test.info}},
您可以使用:{{test.info.0}}来可视化:关键字。
这是你需要的吗?
问候
马西莫
If you see:
[u'keyword'] for example write into template : {{test.info}}
you can use : {{test.info.0}} for visualize : keyword.
Is this that you need?
Regards
Massimo
说 Unicode 值不能在模板中打印出来完全是无稽之谈。我不知道是什么让你这么想。
编码为 utf-8 无需执行任何特殊操作。这是默认设置。如果您看到不同的东西,则说明您在某处配置错误。
更新后编辑不行,还是不明白问题所在。第一个例子就可以了。不需要专门编码为utf-8。当它是 unicode 时,它怎么会“失败”呢? (“当它是 unicode 值时”是什么意思?从 Django 传递到模板的所有值都是 unicode。)
It's complete nonsense to say Unicode values can't be printed out in templates. I have no idea what makes you think that.
Nothing special needs to be done to encode as utf-8. That is the default. If you're seeing something different, you have misconfigured something somewhere.
Edit after update No, still don't understand the problem. The first example just works. There is no need to specifically encode to utf-8. How does it "fail" for you when it's unicode? (And what do you mean by "when it's a unicode value"? All values passed from Django to the template are unicode.)