Django在模板之外人性化? [英] Django humanize outside of template?

查看:108
本文介绍了Django在模板之外人性化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用人性化模块将django模板中的日期/时间转换为更友好的格式.我想知道是否可以在模板之外转换这些内容.例如在views.py函数或models.py类中(意味着在django模板之外).还有其他图书馆可以做到这一点吗?

I know I can use the humanize module to convert date/time to a friendlier format in the django templates. I was wondering if I can convert those things outside the templates. For example in a views.py function or a models.py class(meaning outside of a django template). Is there any other library that can do that?

推荐答案

是的

让我们说您想在views.py中呼叫naturalday

Lets say you want to call naturalday in views.py you would do

from django.contrib.humanize.templatetags.humanize import naturalday
natural_day = naturalday(value)

您可以参考此处的源代码签名和选项

这篇关于Django在模板之外人性化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆