python - Django 模板翻译中的复杂引号转义

标签 python html django

我正在尝试修复以法语查看页面时出现的错误。问题是法语翻译字符串中有撇号,导致 HTML 字符串无法正确关闭。

违规代码:

<select data-bind="
    optstr: [{value: 'none', label: '{% trans "String one" %}'},
             {value: 'open', label: '{% trans "String two" %}'}],
    value: actionType
"></select>

是否有更好的方法来构造它或转义从 trans 调用返回的内容?

最佳答案

我通过单独进行翻译并使用 escapejs 过滤器解决了这个问题。

{% trans "String one" as string_one %}
{% trans "String two" as string_two %}
<select data-bind="
    optstr: [{value: 'none', label: '{{ string_one|escapejs }}'},
             {value: 'open', label: '{{ string_two|escapejs }}'}],
    value: actionType
"></select>

关于python - Django 模板翻译中的复杂引号转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22469235/

相关文章:

python - 使用带有自定义用户模型和 UserManager 的 python-social-auth 创建 django 用户时出现问题

python - Django OAuth 工具包 - 注册用户

django 模板奇怪的行为

python - 合并具有相同值的 numpy 数组/数据帧并保持差异

python - 为什么我的模块在 Python 中运行两次

Python 导入 : AttributeError: 'module' object has no attribute 'test'

javascript - 你如何在整个文档中精确定位一个词或一个选择?

javascript - 在窗口加载时使用 JS 打印 html 表格

python - 如何在Python命令行URL下载器中添加进度条?

html - 当标题位于响应式 View 中的不同列中时,将标题移到图像上方