如何防止 Django 本地化模板中的 ID?
我最近升级到 Django 1.2.5,现在我遇到了本地化问题,特别是数字格式问题。例如,在某些模板中,我打印以下示例:
data-id="{{ form.instance.id }}"
在情况 >= 1000 的情况下,用于评估为:
data-id="1235"
但现在它实际上导致(我的本地化是 pt-BR,我们的小数分隔符是点):
data-id="1.235"
这当然是后来通过ID查询数据库没有找到。使用 |safe
过滤器可以解决问题,但我不愿意找到所有模板中的所有 ID 并保护它们。
通常,我只会本地化浮点数,而不是整数。我不想禁用 L10N,因为所有其他格式都工作正常。 有没有办法在 Django 本地化中做出这种区分? 接受任何其他解决方案。
I have recently upgraded to Django 1.2.5, and now I am having problems with localization, specifically number formatting. For example, in some templates I print the following samples:
data-id="{{ form.instance.id }}"
Which in cases >= 1000, used to evaluate to:
data-id="1235"
But now it actually results in (my localization is pt-BR, our decimal separator is dot):
data-id="1.235"
Which of course is not found when I afterwards query the database by ID. Using a |safe
filter solves the problem, but I'm not willing to find all IDs in all templates and safe them.
Usually, I'll only localize the floating points, not the integers. I don't want to disable L10N, because of all the other formatting that is working fine. Is there a way to make this distinction in Django localization? Any other solution is accepted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也做好本职工作
Also do the job
使用 django 1.2:
或使用 django 1.3:
或(也使用 django 1.3):
with django 1.2:
or, with django 1.3:
or (also with django 1.3):
这并不能真正回答您的问题,但请查看
文档
。它说使用{{ |unlocalize }}
过滤器或:可能有更好的方法,但我认为您可以编写一个方法,为每个模型提供模型中的 id 作为字符串您正在尝试在模板中显示 id。
在你的模板中:
This doesn't really answer your question but check out this section of the
docs
. It says to use{{ |unlocalize }}
filter or:There's probably a better way but I'm thinking that you could write a method that gives you the id as a string in your model for each model you are trying to display the id in a template.
in your template: