How to humanize tags in Django?


Humanize means to make something more humane or human readable. To humanize tags means to makes it easier for the humans to understand. For example, 1800000000 becomes 1.8 billion or 10000 becomes 10,000.

There are simple changes that can be implemented to convert tags to a more human readable format. Django offers template filters that add human touch to data which makes it more appealing to read.

To enable the usage of these filters, django.contrib.humanize should be added to the INSTALLED_APPS in the settings.py file of your project.

INSTALLED_APPS = [
   'reglogin',
   'mlmodel',
   'django.contrib.humanize',
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles'
]

Later, {%load humanize %} should be added to the header section of the template part.

Now, we have enabled the use of humanize filters provided by django in your project.

There are many template filters. Some of them are given below.

  • Apnumber − This filter spells out the numbers from digits 1 to 9 and for all other numbers it returns the same number. Eg: 2 returns two 12 returns 12

  • Intcomma − This filter convers an number(integer or floating point) to a string representation in which digits are comma separated. Eg: 37900 becomes 37,900

  • Intword − Converts a large number to a readable word representation. Eg: 8000000 becomes 8.0 million 1200000000 becomes 1.2 billion

  • Naturalday − Dates nearer to the current date are returned as today, tomorrow, or yesterday.

  • Naturaltime − This filter returns the string representation of how many minutes, seconds or hours has passed since a particular time. Eg: 6th April 2001 12:57:34 becomes now 6th April 2001 12:57:04 becomes 30 seconds ago and so on.

  • Ordinal − This filter converts and integer to its ordinal representation and stores it as a string datatype. Eg: 4 becomes 4th Implementing these filters in a Django project.

To test the working of these filters, we shall work with the filters in a django project. Create a django project and an app in the project. In the views.py file of the django app. The following code should be written.

from django.shortcuts import render
import datetime
def homepage(request):
   current_time = datetime.datetime.now()
   context = {
      'comma': 17937,
      'word': 1800000000,
      'current_time': current_time,
      'day1': current_time - datetime.timedelta(days=1),
   }
   return render(request, 'test.html', context)

Then, the test.html should contain the following data for it to print output.

{% 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>Template Filters to Humanize Data</u></h2>
   <h3>1) intcomma</h3>
   <p>{{ comma }} translates to {{ comma | intcomma }}</p>
   <hr>
   <h3>2) intword</h3>
   <p>{{ word }} translates to {{ word | intword }}</p>
   <hr>
   <h3>3) naturalday</h3>
   <b>Current time is: {{ current_time }}</b>
   <p>{{ day1 }} translates to {{ day | naturalday }}</p>
   <hr>
</body>
</html>

The output obtained will be in the form of −

Template Filters to Humanize Data
1)intcommma
17937 translates to 17.939
2)intword
1800000000 translates to 1.8 billion
3)naturalday
Current time is April 19th 2022, 3:43pm
April 18th 2022, 3:43 pm translates to yesterday.

In this way, we can add filters to templates in Django projects to humanize data. It is especially useful to help users understand the currency exchange or time change.

Updated on: 05-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements