【发布时间】:2015-04-15 06:47:50
【问题描述】:
我有一个模板标签如下,
@register.filter
def get_runtime(date_start=None, date_end=None):
fmt = '%Y-%m-%dT%H:%M:%S'
if date_start and date_end is not None:
date_start = datetime.strptime(date_start, fmt)
date_end = datetime.strptime(date_end, fmt)
return date_end - date_start
else:
return None
我将其传递到我的网站之一,如下所示,
<b>Run Time (min): </b> {{ value.date_start|get_runtime:value.date_end }}<br>
但它显示错误“strptime() 正好需要 2 个参数(1 个给定)” 请帮我解决这个问题。提前致谢
【问题讨论】:
-
value的类型是什么?是模型吗,date_start、date_end是 DateTimeField? -
该代码无法给出该错误。尽管您的
if逻辑中有错误(应该是if date_start is not None and date_end is not None),但如果其中之一是 None,您仍然不会收到该错误 - 您会收到“TypeError: must be str, not None”。 -
@sneawo:是的,它在数据库表中的日期时间
标签: django datetime templatetag