前景提要
HDC调试需求开发(15万预算),能者速来!>>>
大家好,今天遇到一个问题!
就是在tornado模板中需要转换日期字符串的格式!
例如,收到的字符串是'2015-10-11',我需要转换成'Oct 11, 2015'
我用的是strptime和strftime两个函数!
具体如下:
datetime.datetime.strptime('2015-10-11', "%Y-%m-%d").strftime("%b %d, %Y")
在tornado模板我看到datetime关键字是指的是python中的datatime模块,然后我在tornado模板中写了这么一句代码:
<time class="entry-date">{{ datetime.datetime.strptime(passage.mtime, "%Y-%m-%d ").strptime("%b %d, %Y") }}</time>
然后运行的时候程序就报错了,错误提示如下所示:
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 808, in render_string return t.generate(**namespace) File "/usr/local/lib/python2.7/dist-packages/tornado-4.3.dev1-py2.7-linux-x86_64.egg/tornado/template.py", line 345, in generate return execute() File "index_html.generated.py", line 82, in _tt_execute _tt_tmp = datetime.datetime.strptime(passage.mtime, "%Y-%m-%d %H:%M:%S").strptime("%b %d, %Y") # index.html:16 (via base.html:56) TypeError: must be string, not datetime.datetime
我个人分析,感觉是tornado中将datetime关键字解析了两次(datetime.datetime.strptime语句中),请问一下这个该怎么解决啊?