- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Making your own custom filter tags in Django
- How to Humanize numbers with Python?
- How to implement django-material in your Django project?
- How to implement tags in Cypress?
- How to install Django in Anaconda?
- How to use memcached in Django?
- How to import the tags in Azure?
- How to add an UpdateView in Django?
- How to prevent session hijacking in Django?
- How to Create an App in Django?
- How to get documents by tags in MongoDB?
- How to add a Money field in Django?
- How to make a Country field in Django?
- How to add Social Share buttons in Django?
- How to change password of users in Django?
