在变色龙中使用代码块时获取builtins.NameError

时间:2013-03-27 20:58:36

标签: python chameleon template-tal

我希望这段代码能够正常工作:

<div class="footer">
  <? python import time; year = time.strftime('%Y') ?>
  &copy; Copyright 2008-${year}, Agendaless Consulting.
</div>

它失败了:

NameError: year

 - Expression: " python import time; year = time.strftime('%Y') ?>
      &copy; Copyright 2008-${year}, Agendaless Consulting."
 - Filename:   /tmp/foo/foo/foo/foo/templates/mytemplate.pt
 - Location:   (74:8)
 - Source:     ... python import time; year = time.strftime('%Y') ?>
                  ^
 - Arguments:  view: <function my_view at 0x7f48d0bca3b0>
               repeat: {...} (0)
               req: <Request - at 0x7f48d29173d0>
               renderer_name: templates/mytemplate.pt
               project: foo
               request: <Request - at 0x7f48d29173d0>
               renderer_info: <RendererHelper - at 0x7f48d0bd1450>
               context: <DefaultRootFactory None at 0x7f48d2917450>

作为背景,我们的想法是避免每年更新一年:

<div class="footer">
  &copy; Copyright 2008-2012, Agendaless Consulting.
</div>

1 个答案:

答案 0 :(得分:0)

第一个?python之间不应该有空格。以下效果很好:

<div class="footer">
  <?python import time; year = time.strftime('%Y') ?>
  &copy; Copyright 2008-${year}, Agendaless Consulting.
</div>

怪异。