humanize-tags-in-django

The word ‘humanize’ means to make something more humane or civilized. The humanize tags in Django are a set of template filters that are used to give a ‘final touch’ to data that appears in a more readable format to humans than without using it.

For example, you have a date and time set in one of the tasks, and you want to convert it into how long it has been in seconds, minutes, or hours since the task has been created.

In this scenario, we can simply use naturaltime the template filter provided by ‘django.contrib.humanize’ inside the HTML document.

Let at present the time is 27 April 2021 at 10:00:50. Here is 27 April 2021 at 10:00:00 using naturaltime filter translated into 50 seconds ago.

I assumed that you already set up the Django project and are ready to use Django humanize tags. To demonstrate the example right below, I am using the latest Django version 3.2  at the time of writing this post.

Installation

To install Django Humanize, you have to follow just two steps and use those filters according to your data.

Step 1:

First, add 'django.contrib.humanize' to  INSTALLED_APPS  in the settings.py file of your Django project.

...
# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    # add this line to use humanize tags
    'django.contrib.humanize',

    # local apps
    'myapp.apps.MyappConfig',
]
...

 

Step 2:

Secondly, at the header section of the template add  {% load humanize %}  in-built template tag provided by ‘django.contrib.humanize’.

{% load humanize %}
<!DOCTYPE html>
<html lang="en">
<head>
...

 

Template Filters

The template filters available when using Humanize tags are:

1) apnumber

This filter converts single-digit numbers 1-9 into their equivalent word. For more than one-digit number, it returns the same number. It can accept either a string or an integer representation of an integer.

For example:

  • 0 becomes 0
  • 7 becomes seven
  • 77 becomes 77

 

2) intcomma

This filter converts the integer or float type numbers to a string containing commas after every three digits.

For example:

  • 1234 becomes 1,234
  • 123456.7 becomes 123,456.7
  • 100000000 becomes 100,000,000

3) intword

This filter converts the large integer numbers into a format that is easier to remember i.e in million, billion format.

For example:

  • 45785 becomes 45785(For less than 1 million it returns the same number)
  • 10000000 becomes 10.0 million
  • 1500000000 becomes 1.5 billion

 

4) naturalday

This filter converts the date in the format of days: today, tomorrow, and yesterday. Any date that does not fall in those days simply returns the same date as passed.

For example, today’s date is 27 Apr 2021

  • 26 Apr 2021 becomes yesterday
  • 27 Apr 2021 becomes today
  • 28 Apr 2021 becomes tomorrow
  • 05 Aug 2021 becomes 05 Aug 2021

 

5) naturaltime

This filter converts the DateTime values into a string representing how many hours, minutes, or seconds ago it was held.

For example: at present DateTime is 27 Apr 2021 13:15:00

  • 27 Apr 2021 13:15:00 becomes now
  • 27 Apr 2021 13:14:30 becomes 30 seconds ago
  • 25 Apr 2021 12:15:00 becomes 2 days, 1 hour ago

 

6) ordinal

This filter converts the integer number into its ordinal form as that form is used to describe the position in a particular list of items.

For example:

  • 1 becomes 1st
  • 7 becomes 7th
  • 41 becomes 41st

 

Implementation

views.py

from django.shortcuts import render
import datetime


def homepage(request):
    current_time = datetime.datetime.now()
    context = {
        # apnumber
        'ap_first': 1,
        'ap_second': 7,
        # intcomma
        'comma_first': 1234,
        'comma_second': 123456.7,
        # intword
        'word_first': 10000000,
        'word_second': 1500000000,
        # naturalday
        'current_time': current_time,
        'day_first': current_time,
        'day_second': current_time - datetime.timedelta(days=1),
        'day_third': current_time + datetime.timedelta(days=1),
        # naturaltime
        'time_first': current_time - datetime.timedelta(days=7),
        'time_second': current_time + datetime.timedelta(days=600),
        # ordinal
        'ord_first': 7,
        'ord_second': 41,
    }
    return render(request, 'test.html', context)

 

test.html

{% load humanize %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>How to Use Humanize Tags in Django</title>
</head>
<body>
<h2><u>Humanize Template Filters</u></h2>
<h3>1) apnumber</h3>
<p>{{ ap_first }} becomes {{ ap_first | apnumber }}</p>
<p>{{ ap_second }} becomes {{ ap_second | apnumber }}</p>
<hr>
<h3>2) intcomma</h3>
<p>{{ comma_first }} becomes {{ comma_first | intcomma }}</p>
<p>{{ comma_second }} becomes {{ comma_second | intcomma }}</p>
<hr>
<h3>3) intword</h3>
<p>{{ word_first }} becomes {{ word_first | intword }}</p>
<p>{{ word_second }} becomes {{ word_second | intword }}</p>
<hr>
<h3>4) naturalday</h3>
<b>Current time is: {{ current_time }}</b>
<p>{{ day_first }} becomes {{ day_first | naturalday }}</p>
<p>{{ day_second }} becomes {{ day_second | naturalday }}</p>
<p>{{ day_third }} becomes {{ day_third | naturalday }}</p>
<hr>
<h3>5) naturaltime</h3>
<b>Current time is: {{ current_time }}</b>
<p>{{ time_first }} becomes {{ time_first | naturaltime }}</p>
<p>{{ time_second }} becomes {{ time_second | naturaltime }}</p>
<hr>
<h3>6) ordinal</h3>
<p>{{ ord_first }} becomes {{ ord_first | ordinal }}</p>
<p>{{ ord_second }} becomes {{ ord_second | ordinal }}</p>
<hr>

</body>
</html>

Output

 

output-of-django-humanize-template-filters

 

Conclusion

Hence, in this blog post, we successfully implemented humanize tags in our Django project. It helps to make our data more human-friendly and I suggest you use those filters whenever it is possible. If you are stuck in any section feel free to ask your questions in the comment section right below.

Reference 

Django official documentation

Happy Coding 🙂

Leave a Reply

Your email address will not be published.