How to use Humanize Tags in Django?

Published On: 16/09/2022 | Category: Django

Hi Dev,

This is a short guide on how to use humanize tags in django?. I would like to share with you django humanize in view. we will help you to give example of django humanize tags example. I explained simply about django humanize tags example tutorial. Follow bellow tutorial step of django humanize time.

First of all what is a humanize so humanize means to make something more humane or human readable. you have a date and time set in one of the tasks, and you optate to convert it into how long it has been in seconds, minutes, or hours since the task has been engendered.

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

Here i explained simply step by step example of how to use humanize tags in django.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example

Step 2: Create a App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python3 startapp core

Step 3: Update

Then update INSTALLED_APPS within our file to notify Django about the app, and also add this `django.contrib.humanize` in file so our setting file look like this..

    # add this line to use humanize tags

So, there are so many humanize filter in `django.contrib.humanize` so all of list 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: 48500 becomes 48,500
  • Intword : Converts a large number to a readable word representation. Eg: 4000000 becomes 4.0 million 1600000000 becomes 1.6 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: 16th September 2022 12:57:34 becomes now 16th September 2022 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.

Step 4: Creating the Views

In this step, we need to configure views. The home page will just be a template. function is basically get the different humanize tag value open the core/ file and add:

from django.shortcuts import render
import datetime

# Create your views here.
def home(request):

    current_time =

    context = {
        # apnumber
        'ap_first': 1,
        'ap_second': 5,

        # intcomma
        'comma_first': 1111,
        'comma_second': 111222.3,

        # 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': 6,
        'ord_second': 40,

    return render(request, 'home.html', context)

Step 5: Creating the Templates

Next, then with your text editor create new templates files: core/templates/home.html file and the add:

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

{% load humanize %}
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css">
    <style type="text/css">
            background-color: #f7fcff;
    <div class="container mt-5">
        <div class="row d-flex justify-content-center">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h3>How to Use Humanize Tags in Django? - <span class="text-primary"></span></h3>
                    <div class="card-body">
                        <table class="table table-bordered">
                                    <th colspan="2">apnumber</th>
                                    <td>{{ ap_first }} becomes {{ ap_first | apnumber }}</td>
                                    <td>{{ ap_second }} becomes {{ ap_second | apnumber }}</td>
                                    <th colspan="2">intcomma</th>
                                    <td>{{ comma_first }} becomes {{ comma_first | intcomma }}</td>
                                    <td>{{ comma_second }} becomes {{ comma_second | intcomma }}</td>
                                    <th colspan="2">intword</th>
                                    <td>{{ word_first }} becomes {{ word_first | intword }}</td>
                                    <td>{{ word_second }} becomes {{ word_second | intword }}</td>
                                    <th colspan="2">naturalday</th>
                                    <td><strong>Current time is: {{ current_time }}</strong><br>{{ day_first }} becomes {{ day_first | naturalday }}</td>
                                    <td>{{ day_second }} becomes {{ day_second | naturalday }}</td>
                                    <th colspan="2">naturaltime</th>
                                    <td><strong>Current time is: {{ current_time }}</strong><br>{{ time_first }} becomes {{ time_first | naturaltime }}</td>
                                    <td>{{ time_second }} becomes {{ time_second | naturaltime }}</td>
                                    <th colspan="2">ordinal</th>
                                    <td>{{ ord_first }} becomes {{ ord_first | ordinal }}</td>
                                    <td>{{ ord_second }} becomes {{ ord_second | ordinal }}</td>

Step 6: Creating URLs

In this section, we need a file within the core app however Django doesn't create one for us with the startapp command. Create core/ with your text editor and paste below code.

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),

Next, we require to add a URL path for our example app which can be done by importing include and setting a path for it.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('core.urls')),

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/ address with a web browser.

I hope it will help you....

Happy Coding!