Humanize - Django Utils

Django comes with a set of template filters to add a “human touch” to your data. It is used to translate numbers and dates into a human readable format.

Installation

  • Add django.contrib.humanize to the INSTALLED_APPS setting:
INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Humanize
    'django.contrib.humanize',
]

Now in the template, load the template tags:

{% load humanize %}

Usage

Using it very straightforward.

1. naturaltime

{% extends 'base.html' %}

{% load humanize %}

{% block content %}
  <ul>
    {% for message in messages %}
      <li>
        {{ message }}
        <small>{{ message.date|naturaltime }}</small>
      </li>
    {% empty %}
      <li>You have no new message.</li>
    {% endfor %}
  </ul>
{% endblock %}

For datetime values, returns a string representing how many seconds, minutes or hours ago it was – falling back to the timesince format if the value is more than a day old. In case the datetime value is in the future the return value will automatically use an appropriate phrase.

Examples (when ‘now’ is 05 Oct 2022 16:30:00):

  • 05 Oct 2022 16:30:00 becomes now.
  • 05 Oct 2022 16:29:31 becomes 29 seconds ago.
  • 05 Oct 2022 16:29:00 becomes a minute ago.
  • 05 Oct 2022 16:25:35 becomes 4 minutes ago.
  • 05 Oct 2022 15:30:29 becomes 59 minutes ago.
  • 05 Oct 2022 15:30:01 becomes 59 minutes ago.
  • 05 Oct 2022 15:30:00 becomes an hour ago.
  • 05 Oct 2022 13:31:29 becomes 2 hours ago.
  • 04 Oct 2022 13:31:29 becomes 1 day, 2 hours ago.
  • 04 Oct 2022 13:30:01 becomes 1 day, 2 hours ago.
  • 04 Oct 2022 13:30:00 becomes 1 day, 3 hours ago.
  • 05 Oct 2022 16:30:30 becomes 30 seconds from now.
  • 05 Oct 2022 16:30:29 becomes 29 seconds from now.
  • 05 Oct 2022 16:31:00 becomes a minute from now.
  • 05 Oct 2022 16:34:35 becomes 4 minutes from now.
  • 05 Oct 2022 17:30:29 becomes an hour from now.
  • 05 Oct 2022 18:31:29 becomes 2 hours from now.
  • 06 Oct 2022 16:31:29 becomes 1 day from now.
  • 14 Oct 2022 18:31:29 becomes 1 week, 2 days from now.

2. apnumber

For numbers 1-9, returns the number spelled out. Otherwise, returns the number. This follows Associated Press style.

Examples:

  • 1 becomes one.
  • 2 becomes two.
  • 10 becomes 10.

You can pass in either an integer or a string representation of an integer.


3. intcomma

Converts an integer or float (or a string representation of either) to a string containing commas every three digits.

Examples:

  • 4500 becomes 4,500.
  • 4500.2 becomes 4,500.2.
  • 45000 becomes 45,000.
  • 450000 becomes 450,000.
  • 4500000 becomes 4,500,000.

Format localization will be respected if enabled, e.g. with the ‘de’ language:

  • 45000 becomes ‘45.000’.
  • 450000 becomes ‘450.000’.

4. intword

Converts a large integer (or a string representation of an integer) to a friendly text representation. Translates 1.0 as a singular phrase and all other numeric values as plural, this may be incorrect for some languages. Works best for numbers over 1 million.

Examples:

  • 1000000 becomes 1.0 million.
  • 1200000 becomes 1.2 million.
  • 1200000000 becomes 1.2 billion.
  • -1200000000 becomes -1.2 billion.

Values up to 10^100 (Googol) are supported.

Format localization will be respected if enabled, e.g. with the ‘de’ language:

  • 1000000 becomes ‘1,0 Million’.
  • 1200000 becomes ‘1,2 Millionen’.
  • 1200000000 becomes ‘1,2 Milliarden’.
  • -1200000000 becomes ‘-1,2 Milliarden’.

5. naturalday

For dates that are the current day or within one day, return today, tomorrow or yesterday, as appropriate. Otherwise, format the date using the passed in format string.

Argument: Date formatting string as described in the date tag.

Examples (when ‘today’ is 05 Oct 2022):

  • 04 Oct 2022 becomes yesterday.
  • 05 Oct 2022 becomes today.
  • 06 Octb 2022 becomes tomorrow.

Any other day is formatted according to given argument or the DATE_FORMAT setting if no argument is given.


6. ordinal

Converts an integer to its ordinal as a string.

Examples:

  • 1 becomes 1st.
  • 2 becomes 2nd.
  • 3 becomes 3rd.

You can pass in either an integer or a string representation of an integer.

Explore More Django Posts

Efficient Django Project Settings with Split Settings Library

Learn how to efficiently manage your Django project settings with the Split Settings library. Use environment variables, keep sensitive information i…

Read More
Integrating Flake8 with Django: Best Practices

Learn how to integrate Flake8 with Django projects and enforce code quality. Follow our step-by-step guide and optimize your Django workflow with Fla…

Read More
Django Authentication and Authorization with JWT

Learn how to implement JSON Web Token (JWT) based authentication and authorization in Django web applications with step-by-step guide and code exampl…

Read More
Best Practices for Django Development: Tips and Tricks

Learn the best practices for Django development, including project structure, code organization, testing, and deployment. Build high-quality web apps.

Read More
Django Middleware: Tips, Tricks and Examples

Learn how to use Django Middleware to improve your app's performance and security. Includes examples and best practices.

Read More
Django Production Deployment: Best Practices & Checklist

Learn the best practices and checklist for deploying a Django application to production. Includes tips on web servers, databases, caching, security, …

Read More