views:

41

answers:

2

Hi All,

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 time 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.

A: 

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))
damir
A: 

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 }}
Daniel Roseman
Didn't know it. Thanks a lot Daniel.
anand
However, "now" does work without passing with context. It was just not working with "timesince". Because it was working fine as {% now "jS F Y H:i" %} for me.
anand
Completely different thing. That's using the default built-in template *tag*, `{% now %}`, rather than using the value of `now` as a variable.
Daniel Roseman