V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
bwangel
V2EX  ›  Python

tornado 模板中如何使用 datetime 模块

  •  
  •   bwangel ·
    bwangelme · 2015-10-11 18:35:05 +08:00 · 2229 次点击
    这是一个创建于 3334 天前的主题,其中的信息可能已经有所发展或是发生改变。

    <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
    

    我在这个网址中看到了 datetime 的使用示例,照着实例来会出问题,请问正确的方法应该如何写!

    1 条回复    2015-10-11 18:41:45 +08:00
    bwangel
        1
    bwangel  
    OP
       2015-10-11 18:41:45 +08:00
    好吧,我错了,模板语法写错了!

    `<time class="entry-date">{{ datetime.date.today().year }}</time>`
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3158 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:40 · PVG 21:40 · LAX 05:40 · JFK 08:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.