Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Django – Making a Django website more human-like using Humanizer
In this article, we will see what is humanizer and how to use it in Django. Humanizer is a built-in Django filter that adds a human touch to your project by converting raw data into more readable formats. It's one of the most useful template filters in Django.
Humanizer helps convert numbers from numerical figures to words, add commas between large numbers, or convert numbers to readable formats like million or billion. Let's create a Django project to demonstrate its usage.
Project Setup
First, create a Django project and an app, then configure the project URLs ?
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include("humanizeproj.urls")),
path('admin/', admin.site.urls),
]
In your app's urls.py, add the following ?
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home")
]
Configuring Templates and Settings
Create a templates folder and configure it in settings.py ?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'humanizeproj/templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
In settings.py, add your app and the humanize contrib module ?
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'humanizeproj', # your app name
'django.contrib.humanize' # required for humanize filters
]
Creating Views and Templates
In views.py, create a simple view ?
from django.shortcuts import render
def home(request):
return render(request, "home.html")
In home.html, demonstrate the humanize filters ?
<!DOCTYPE html>
<html>
<head>
<title>Django Humanize Demo</title>
</head>
<body>
{% load humanize %}
<h2>AP Number Filter (apnumber):</h2>
<p>1 becomes: {{ "1" | apnumber }}</p>
<p>2 becomes: {{ "2" | apnumber }}</p>
<p>10 becomes: {{ "10" | apnumber }}</p>
<h2>Integer Comma Filter (intcomma):</h2>
<p>1000 becomes: {{ "1000" | intcomma }}</p>
<p>200000 becomes: {{ "200000" | intcomma }}</p>
<p>10000000 becomes: {{ "10000000" | intcomma }}</p>
<h2>Integer Word Filter (intword):</h2>
<p>1000000000 becomes: {{ "1000000000" | intword }}</p>
<p>20000000000 becomes: {{ "20000000000" | intword }}</p>
<p>10000000 becomes: {{ "10000000" | intword }}</p>
</body>
</html>
Humanize Filters Explained
The three main humanize filters demonstrated are:
- apnumber ? Converts numbers 1-9 to words (e.g., "1" becomes "one")
- intcomma ? Adds commas to large numbers for better readability (e.g., "1000" becomes "1,000")
- intword ? Converts large numbers to words like million, billion (e.g., "1000000" becomes "1.0 million")
Output
When you run the server and visit the homepage, you'll see the humanized output:
AP Number Filter (apnumber): 1 becomes: one 2 becomes: two 10 becomes: 10 Integer Comma Filter (intcomma): 1000 becomes: 1,000 200000 becomes: 200,000 10000000 becomes: 10,000,000 Integer Word Filter (intword): 1000000000 becomes: 1.0 billion 20000000000 becomes: 20.0 billion 10000000 becomes: 10.0 million
Conclusion
Django's humanize filters make your website more user-friendly by presenting data in readable formats. Use apnumber for small numbers, intcomma for large numbers with commas, and intword for converting large numbers to words like million or billion.
