tornado 模板中如何使用 datetime 模块

2015-10-11 18:35:05 +08:00
 bwangel
`<time class="entry-date">{{% datetime.date.today().year %}}</time>`

我在一个 tornado 模板中使用的 datetime 模块,如上所示!

但是运行的时候出错,错误代码如下所示:

```
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado-4.3.dev1-py2.7-linux-x86_64.egg/tornado/web.py", line 1411, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "./blog.py", line 87, in get
self.render("index.html", aside=True, passages=passages)
File "/usr/local/lib/python2.7/dist-packages/tornado-4.3.dev1-py2.7-linux-x86_64.egg/tornado/web.py", line 701, in render
html = self.render_string(template_name, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tornado-4.3.dev1-py2.7-linux-x86_64.egg/tornado/web.py", line 805, in render_string
t = loader.load(template_name)
File "/usr/local/lib/python2.7/dist-packages/tornado-4.3.dev1-py2.7-linux-x86_64.egg/tornado/template.py", line 424, in load
self.templates[name] = self._create_template(name)
File "/usr/local/lib/python2.7/dist-packages/tornado-4.3.dev1-py2.7-linux-x86_64.egg/tornado/template.py", line 452, in _create_template
template = Template(f.read(), name=name, loader=self)
File "/usr/local/lib/python2.7/dist-packages/tornado-4.3.dev1-py2.7-linux-x86_64.egg/tornado/template.py", line 314, in __init__
"exec", dont_inherit=True)
File "index_html.generated.py", line 82
_tt_tmp = % datetime.date.today().year % # index.html:16 (via base.html:56)
^
SyntaxError: invalid syntax
```

我在[这个网址]( https://gavinroy.com/posts/tornado-tip-of-the-day-38-variables-exposed-i.html)中看到了 datetime 的使用示例,照着实例来会出问题,请问正确的方法应该如何写!
2181 次点击
所在节点    Python
1 条回复
bwangel
2015-10-11 18:41:45 +08:00
好吧,我错了,模板语法写错了!

`<time class="entry-date">{{ datetime.date.today().year }}</time>`

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/227184

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX