Django: How to create "Published" and "Last edited" fields?

71 Views Asked by At

I'm writing a blog for my portfolio and I wanna compare (in the Django template) the date I published an article with the date I edited it in order to display the "Edit date" only if it was edited before.

The problem is: I don't wanna test for every single date field, like year, month, day, hour, minute, etc., but if I simply compare the two values, it'll always display both values, since the time precision goes beyond seconds, making the dates different even though I never edited that particular post.

TL;DR How to compare dates when their values will always differ, given the precision Django takes time, without using an iffor every value (year, month, day, hour, minute, second)?

What I tried: I successfully accomplished solving my main problem, but my code looks something like if this == that and if this == that etc.. It doesn't look pretty and I don't wanna code this for every website I build. It looks like there's a better way to solve it.

1

There are 1 best solutions below

0
David Wenzel On

for comparison you can check the diff between two datetime objects

(edited-created).total_seconds()

as suggested by John Gordon you should just just leave it empty till you actually have edited the post