How to use Humanize Tags in Django?

Hi Dev,
This guide will help you understand Django humanize tags with easy examples and implementation. If you're looking for how to make dates, numbers, and times more human-readable in Django templates, then this tutorial is perfect for you.
With django.contrib.humanize
, you can display values like “5 minutes ago”, “10 million”, “yesterday”, or “1,000,000” using built-in filters such as naturaltime, intword, intcomma, apnumber, and ordinal.
Step 1: Create a Project
In this step, we’ll create a new Django project using the django-admin
.
django-admin startproject example
Step 2: Create an App
Now we'll create a single app called core.
python3 manage.py startapp core
Step 3: Update setting.py
Add your app and 'django.contrib.humanize'
to INSTALLED_APPS
.
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'core', # add this line to use humanize tags 'django.contrib.humanize', ]
Available Humanize Filters in Django:
- apnumber: 2 → two
- intcomma: 48500 → 48,500
- intword: 4000000 → 4.0 million
- naturalday: datetime → today, yesterday, tomorrow
- naturaltime: datetime → 2 minutes ago
- ordinal: 4 → 4th
Step 4: Creating the Views
from django.shortcuts import render import datetime def home(request): current_time = datetime.datetime.now() context = { 'ap_first': 1, 'ap_second': 5, 'comma_first': 1111, 'comma_second': 111222.3, 'word_first': 10000000, 'word_second': 1500000000, 'current_time': current_time, 'day_first': current_time, 'day_second': current_time - datetime.timedelta(days=1), 'day_third': current_time + datetime.timedelta(days=1), 'time_first': current_time - datetime.timedelta(days=7), 'time_second': current_time + datetime.timedelta(days=600), 'ord_first': 6, 'ord_second': 40, } return render(request, 'home.html', context)
Step 5: Creating the Templates
File: core/templates/home.html
{% load humanize %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Stuffcoder.com</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css"> <style type="text/css"> body{ background-color: #f7fcff; } </style> </head> <body> <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">Stuffcoder.com</span></h3> </div> <div class="card-body"> <table class="table table-bordered"> <tbody> <tr> <th colspan="2">apnumber</th> <td>{{ ap_first }} becomes {{ ap_first | apnumber }}</td> <td>{{ ap_second }} becomes {{ ap_second | apnumber }}</td> </tr> <tr> <th colspan="2">intcomma</th> <td>{{ comma_first }} becomes {{ comma_first | intcomma }}</td> <td>{{ comma_second }} becomes {{ comma_second | intcomma }}</td> </tr> <tr> <th colspan="2">intword</th> <td>{{ word_first }} becomes {{ word_first | intword }}</td> <td>{{ word_second }} becomes {{ word_second | intword }}</td> </tr> <tr> <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> </tr> <tr> <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> </tr> <tr> <th colspan="2">ordinal</th> <td>{{ ord_first }} becomes {{ ord_first | ordinal }}</td> <td>{{ ord_second }} becomes {{ ord_second | ordinal }}</td> </tr> </tbody> </table> </div> </div> </div> </div> </div> </body> </html>
Step 6: Creating URLs
core/urls.py
and example/urls.py
# core/urls.py from django.urls import path from . import views urlpatterns = [ path('', views.home), ]
# example/urls.py from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('core.urls')), ]
Run the Server
Start Django development server:
python manage.py runserver
Visit http://localhost:8000/ in your browser to test.
Frequently Asked Questions (FAQ)
1. What is the use of humanize in Django?
Django humanize makes data like dates and numbers more readable by humans, e.g., "2 minutes ago", "10 million".
2. How do I enable humanize filters in Django?
Add 'django.contrib.humanize'
in your INSTALLED_APPS
and load it in your template using {% load humanize %}
.
3. What is naturaltime in Django?
naturaltime
filter displays relative time strings like "3 minutes ago", "2 hours ago", etc.
4. Can I use humanize in Django views?
No, humanize filters are designed to be used inside Django templates only.
5. Does humanize support localization?
Yes, humanize filters support localization when enabled via Django settings and translations.
I hope this guide helps you get started with Django Humanize Tags.
Happy Coding! – Stuffcoder