Django Templates: How to show the time differences between a Datastore time and current time?
I am working on a django- Google app engine project. A user inserts some value and the datetime field is auto filled using google app engine DateTimeProperty(auto_now_add=True) property. Now, I have to display on the template the difference of this time and the current time when the user is viewing the result.
For e.g. if the inserted t开发者_开发百科ime was 5:00 and the user is viewing the post at 6:00, the result should be 1 hour. Is there any builtin filter available with the Django templates to perform the same? I tried timesince as:
{{ topic.creation_date| timesince: now }}
where creation_date is a datetime field. But its not working.
Please suggest.
Thanks in advance.
Do you have a variable called "now" in your context? If not, that will fail.
However, you don't actually need one, because if you call the timesince
filter without an argument, it will use the current time. So just do:
{{ topic.creation_date|timesince }}
why don't you use time.time()? And for creation date you first have to insert given row into database. That means you can read it.
from datetime import timedelta
yourmodel.save()
cdate= yourmodel.creation_date
seconds = time.time() - time.mktime(cdate.timetuple())
timediff = str(timedelta(seconds=seconds))
精彩评论