app.py - >
def pretty_date(time=False):
from datetime import datetime
now = datetime.now()
if type(time) is int:
diff = now - datetime.fromtimestamp(time)
elif isinstance(time,datetime):
diff = now - time
elif not time:
diff = now - now
second_diff = diff.seconds
day_diff = diff.days
if day_diff < 0:
return ''
if day_diff == 0:
if second_diff < 10:
return "just now"
if second_diff < 60:
return str(second_diff) + " seconds ago"
if second_diff < 120:
return "a minute ago"
if second_diff < 3600:
return str(second_diff / 60) + " minutes ago"
if second_diff < 7200:
return "an hour ago"
if second_diff < 86400:
return str(second_diff / 3600) + " hours ago"
if day_diff == 1:
return "Yesterday"
if day_diff < 7:
return str(day_diff) + " days ago"
if day_diff < 31:
return str(day_diff / 7) + " weeks ago"
if day_diff < 365:
return str(day_diff / 30) + " months ago"
return str(day_diff / 365) + " years ago"
和HTML模板 - &gt;
{{pretty_date(post.time)}}
给出错误
UndefinedError:&#39; pretty_date&#39;未定义
有没有办法可以打电话给它?相同的app.py呈现html页面顺便说一句。
答案 0 :(得分:0)
如果我没弄错的话,你必须通过关键字参数在render_template
调用中传递你想要在HTML代码中使用的任何内容(函数,变量等)。
示例:render_template("main.html", pretty_date=pretty_date)