【问题标题】:suppose output python list as javascript list in html, but the char "'" is printed假设将 python 列表输出为 html 中的 javascript 列表,但打印了字符“'”
【发布时间】:2013-06-08 04:31:03
【问题描述】:

我有一个python列表:categoriesPython,即['Firefox 2.0', 'Firefox 3.0', 'Firefox 3.5', 'Firefox 3.6', 'Firefox 4.0'], 我做到了(我正在使用 web.py):

data: {
       name: 'Firefox versions',
       categories: $categoriesPython,
       version: [0.20, 0.83, 1.58, 13.12, 5.43]
      }

我希望得到:

data: {
       name: 'Firefox versions',
       categories: ['Firefox 2.0', 'Firefox 3.0', 'Firefox 3.5', 'Firefox 3.6', 'Firefox 4.0'],
       version: [0.20, 0.83, 1.58, 13.12, 5.43]
      }

但是,我得到了:

data: {
       name: 'Firefox versions',
       categories: ['Firefox 2.0', 'Firefox 3.0', 'Firefox 3.5', 'Firefox 3.6', 'Firefox 4.0'],
       version: [0.20, 0.83, 1.58, 13.12, 5.43]
      }

1。因为数据是Json格式,所以不能有赋值,例如:

    data: {
       name: 'Firefox versions',
       categorieData.push('$categoriesPython');
       categories: categorieData,
       version: [0.20, 0.83, 1.58, 13.12, 5.43]
      }

另外,我不想将python列表转换为javascript列表,因为列表有超过200个值,如果转换,必须有超过200个categorieData.push('$categoriesPython');,代码将是多余的。

我怎样才能得到预期的结果?

【问题讨论】:

  • 似乎有些东西错误地将' 编码为'(一个HTML 字符引用)。
  • 你是如何创建 JSON 的?
  • 我觉得你可以用$:categoriesPython,试试看告诉我们。
  • 使用 $:categoriesPython,谢谢大家!

标签: javascript python json web.py


【解决方案1】:

Filtering 上的文档看来,

$:categoriesPython

可能会成功

默认情况下,Templetor 使用 web.websafe 过滤器进行 HTML 编码。

render.hello("1

要关闭过滤器,请在 $ 之后使用 :。例如:

以下内容不会被html转义。 $:form.render()

【讨论】:

  • 它有效,但我得到:[u'Firefox 2.0', u'Firefox 3.0', u'Firefox 3.5', u'Firefox 3.6', u'Firefox 4.0'],我该怎么做删除 u 前缀?
  • 是的,我修复了:stackoverflow.com/questions/1207457/…
  • 顺便说一句,您无需执行该答案中的操作即可将(您的示例)转换为非 Unicode 字符串。只需执行 str(item) 其中 item 是您当前的 Unicode 字符串。
  • 我在 html 部分做了 $str(item),但我得到“全局名称'str'未定义”...
猜你喜欢
  • 2015-06-02
  • 2020-07-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
相关资源
最近更新 更多