Mako 在有效代码上给出 SyntaxError?

发布于 2024-10-31 22:30:37 字数 1164 浏览 1 评论 0原文

我试图在 Mako 模板中使用一段代码,但无论我在代码块中放入什么,Mako 都坚持认为这是一个语法错误。

这是有问题的块的片段:

  <td class="col_sm_space">&nbsp;</td>
  <%
      if session.dist == "metric":
          delta_distance = "%.2fkm" % (trk["d_distance"] / 1000.0)
          delta_fuel = "%.2fl" % (trk["d_fuel"])
          delta_co2 = "%.2fg" % (trk["d_co2"])
          delta_co2_rate = "%.2fg/l" % trk["d_co2_rate"])
          trip_av_speed = "%dkm/h" % int(trk["trip_av_speed"])
          trip_peak_speed = "%dkm/h" % int(trk["trip_peak_speed"])
  %>
  <td class="col_field" title="${delta_distance}">${trk["trip_distance"]}</td>

我在 if session.dist == "metric": 行上收到语法错误,尽管我可以用任何内容替换它(例如 foo = "bar") 它仍然给我错误。

Mako 返回:

SyntaxException:(SyntaxError) 无效语法(第 5 行)('if session.dist == "metric":\\n delta_distance = ') in file '' at line: 271 char: 9\n, Referer:

271 行是开头 <%。显然,Char 9 将是下一行 if 的开头。

奇怪的是,我在其他页面上使用了完全相同的设置,而且这些设置都很好 - 只是这里不行。

我在这里遗漏了什么明显的东西吗?

I'm trying to use a block of code within a mako template, yet no matter what I put in the block, Mako is adamant it's a syntax error.

Here's a snippet of the block in question:

  <td class="col_sm_space"> </td>
  <%
      if session.dist == "metric":
          delta_distance = "%.2fkm" % (trk["d_distance"] / 1000.0)
          delta_fuel = "%.2fl" % (trk["d_fuel"])
          delta_co2 = "%.2fg" % (trk["d_co2"])
          delta_co2_rate = "%.2fg/l" % trk["d_co2_rate"])
          trip_av_speed = "%dkm/h" % int(trk["trip_av_speed"])
          trip_peak_speed = "%dkm/h" % int(trk["trip_peak_speed"])
  %>
  <td class="col_field" title="${delta_distance}">${trk["trip_distance"]}</td>

I'm getting the syntax error on the if session.dist == "metric": line, although I could replace this with anything (Such as foo = "bar") and it still gives me the error.

Mako is returning:

SyntaxException: (SyntaxError) invalid syntax (line 5) ('if session.dist == "metric":\\n delta_distance = ') in file '<snipped>' at line: 271 char: 9\n, referer: <snipped>

Line 271 is the opening <%. Char 9 would be the beginning of the if on the next line, apparently.

Oddly, I'm using this exact same setup on other pages, and it's fine with those - just not here.

Anything glaringly obvious I'm missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乱了心跳 2024-11-07 22:30:37

我对此有过一次非常令人沮丧的经历。至少就我而言,错误报告是完全错误的。它指向一个 python 块的第一行,就像你的一样,而实际错误是在 50 行之后的另一个 python 块中。

很可能,你有一个简单的语法错误,比如错误的缩进或 if 语句后缺少冒号......我无法给你任何比用细齿梳理你的 python 更好的调试建议。如果可以在 Mako 之外对 python 块进行单元测试,那可能会有所帮助。

I had a really frustrating experience with this. In my case, at least, the error report was just completely wrong. It pointed to the first line of a python block, like yours, when the actual error was in a different python block, 50 lines later.

Likely, you have a simple syntax error like bad indentation or a missing colon after an if statement... I can't give you any better debugging advice than going through your python with a fine-toothed comb. If it's possible to unit-test your python blocks outside of Mako, that might be helpful.

掩耳倾听 2024-11-07 22:30:37

我的经验是,我有这样的 if 语法:

% if ${use_force_ssl} == 1:

相反,它应该是这样的:

% if use_force_ssl == 1:

希望这对某人有帮助。

My experience was that I had this if syntax:

% if ${use_force_ssl} == 1:

Instead, it was supposed to be like this:

% if use_force_ssl == 1:

Hope this helps someone.

隐诗 2024-11-07 22:30:37

很久以前就有人问过这个问题,但根据记录,这一行缺少左括号“(”,尚不清楚这是否导致问题,但似乎有可能:

delta_co2_rate = "%.2fg/l" % trk["d_co2_rate"])

应该至少是:

delta_co2_rate = "%.2fg/l" % (trk["d_co2_rate"])

This was asked a very long time ago but for the record there is a missing left paren, "(", on this line, its not clear if that is causing the problem but it seems likely:

delta_co2_rate = "%.2fg/l" % trk["d_co2_rate"])

Should be at least:

delta_co2_rate = "%.2fg/l" % (trk["d_co2_rate"])
胡渣熟男 2024-11-07 22:30:37

在mako模板中,
当你使用条件语句时,例如if、for等,它应该是这样的:
% if条件
一些代码
%endif

对于分配,你必须将那个东西嵌入<% %>

如果你遵循这个,你的代码将运行。

In mako templates,
when you are using a conditional statement eg if, for etc, it should be like this:
% if condition
some code
%endif

And for assigment, you have to embed that thing in <% %>

If you will follow this, your code will run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文