How to Display How Long Ago a Post was Created in Django



Python


In this article, we show how to display how long ago a post was created in Django.

Say you have a website where users can post content such as on a website like quora. Or a website where users can upload content such as videos on youtube.

You normally want to know when this post was created. You may want to put the absolute timestamp such as, Published on December 3, 2017.

But other times, you want the relative time it was created, such as 1 hour ago or 2 hours ago, or 30 minutes ago, or 15 seconds.

The same thing is the case for comments. When a user posts a comment, you may want to put the timestamp or you may just want to put when the comment was posted relative to the time now (e.g., 2 hours ago).

So how can we do this in Django?

It's actually not that straightforward and does require a bit of customization. However, if you're creating a site that contains user-generated content, then this is important to learn.

So I do know that Django that there is a timesince filter, timesince.

This can be done through the following code.



Even though this works and returns the time since the post was created, it isn't good, because if I was post was created seconds ago, it doesn't display how many seconds but only 0 minutes. Also if a post was created over an hour ago, it will show the hours and the minutes. Just look at a site like youtube. If a video was uploaded to youtube 2 hours and 14 minutes ago, youtube will just state that it was uploaded 2 hours ago, not 2 hours and 14 minutes. Adding the 14 minutes is just unnecessary and more exact than we need it to be.

We're going to model exactly what youtube does.

If the post was created less than 60 seconds ago, we just display the amount of seconds ago it was created (not 0 minutes like with the Django timesince filter).

If the post was created between 1 minute and 59 minutes ago, we just display the amount of minutes ago it was created.

If the post was created between 1 hour and 23 hours ago, we just display the amount of hours ago it was created (not hours as minute like with the Django timesince filter).

If the post was created between 24 hours and 30 days ago, we just display the amount of days ago it was created.

If the post was created between 30 days and 365 days ago, we just display the number of months ago it was created.

If the post was created more than 365 days ago, we just display the number of years ago it was created.

Again, this is just like youtube.

So let's now get into the code, which is almost exclusively in the models.py file.


models.py File

So in the models.py file, we have a model, Post, which contains the posts that users put on the site.

It has a few fields, the timestamp field being called pub_date (for date published).

This model is shown below.



So let's break this down.

We have a post that has a pub_date field, which tells us when the post was published.

We then create a custom function; we call this function whenpublished. And we pass in the keyword, self, which refers to each instance that is created in the database.

We create a variable, called now, and set it equal to, timezone.now()

This gets the current time.

We then create another variable, diff, which function as the timedelta between the current time and the published date of the post. diff= now - self.pub_date

Now in order to completely customize what is shown, we need to write out if statements for every possible condition.

diff has 3 attributes built-in with Python: days, seconds, and microseconds. These are what we utilize when using if statments.

If diff.days is equal to 0 and diff.seconds is greater than or equal to 0 and if.seconds is less than 60, this means the post was created 0-59 seconds ago. For each condition, we always check to see if the variable is equal to 1, because we don't want to have something like, 1 seconds ago. We want 1 to be singular and all others to be plural.

We just continue this same logic in all the other if statements.

If diff.days is equal to 9 and the seconds are equal to or greater than 60 and less than 3600, then we taken the seconds and divide them by 60, which gives us the minutes. We use the math.floor() function, because we don't want something like a post having been created 3.1 minutes ago. We want an integer. Therefore, we can use one of many functions. I just chose to use the math.floor() function. At first, I was thinking of using the Python round() function, but chose not to because if 1 min and 30 seconds went by, the round() function would round it to 2 minutes, which isn't accurate. Therefore, I chose to use the math.floor() function, which is accurate. Be aware that you must import the math module in order to use the math.floor() function.

If diff.days is equal to 0 and the seconds is equal to or greater than 3600 and less than 86400, then we divide the seconds by 3600, to get the number of hours.

If diff.days is equal to or greater than 1 and less than 30, then we return the number of days that have passed.

If diff.days is equal to or greater than 30 and less than 365, then we divide the number by 30, which gives us the months.

If diff.days is equal to or greater than 365, then we divide this number by 365, to give us the number of years.

So this is what we have in the models.py file.

All we have to do next is put the following code in the template file.

Template File

Now in the template file, all you have to do is use the whenpublished function on the postobj in the detail view, or wherever you want to display it.

This is shown below.



This will now tell us how long ago the post was published.

And this is how we can display how long ago a post was created in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...