DEV Community

Emmanuel Okiche
Emmanuel Okiche

Posted on

Django Quick Tip #1: Human Friendly Timestamps with Django's Humanize

Welcome to this short Django Tip. No long story here since this is a 'Quickie'.
This is what we would achieve:

Tranformed timestamp

To achieve this, we would be using the humanize module which is located in django.contrib.humanize.
The humanize module is a collection of template filters that adds a human touch to our displayed data. Instead of spitting out the exact values in our database, it prettifies it to a nice readable format.

Let's get to work

1. Add the module to your INSTALLED_APPS:

'django.contrib.humanize'
Enter fullscreen mode Exit fullscreen mode

2. Load it up in your template:

{% load humanize %}
Enter fullscreen mode Exit fullscreen mode

3. Apply the filter to your timestamp

{{ viewed_on|naturaltime }}
Enter fullscreen mode Exit fullscreen mode

That's it!

The humanize module also contains other filters. Here's a link to the the documentation.
Now go forth and make your apps human friendly.

Video Version

Top comments (0)