article

Tuesday, May 12, 2020

Django Numbers and Dates Using Humanize - human readable format


Django Numbers and Dates Using Humanize - human readable format

humanize It is used to translate numbers and dates into a human readable format.

To install Django Humanize, add django.contrib.humanize to your INSTALLED_APPS setting:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
    'django.contrib.humanize',
]

Now in the template, load the template tags:

{% load humanize %}



 
#view.py
from django.shortcuts import render, redirect
import datetime

# Create your views here.
def index(request):
    now = datetime.datetime.now()
 
    context = {
  'spellone' : 8,
  'spellonetwo' : 1200000,
  'tree' : 8569,
  'four' : 85,
  'now' : now,
  'otherdate' : now + datetime.timedelta(days=1),
  'otherdatetime' : now + datetime.timedelta(days=1),
  'future' : now + datetime.timedelta(days=2542),
  'past' : now - datetime.timedelta(days=30)
    }
    return render(request, 'index.html', context)
 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
    'django.contrib.humanize', 
]
 
from django.contrib import admin  
from django.urls import path  
from myapp import views  

urlpatterns = [  
    path('admin/', admin.site.urls),  
    path('',views.index),
]
//templates/base.html
<!DOCTYPE html>
<html lang="en">
<head>
    {% load static %}
    <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.css' %}"/>
    <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
 <title>{% block title %}Django Numbers and Dates Using Humanize - human readable format{% endblock %}</title>
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand">Cairocoders</a>
        </div>
    </nav>
 <div class="container">
    <div class="col-md-12 well">
        <h3 class="text-primary">Django Numbers and Dates Using Humanize - human readable format</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        {% block content %}
 
        {% endblock %}
    </div>
    </div>
</body>
</html>
//templates/index.html
{% extends 'base.html' %}

{% load humanize %}

{% block content %}

  <h3>Spell : {{ spellone|apnumber}} </h3>
  <h3>Spell : {{ spellonetwo|intword}} </h3>
  <h3>Commas : {{ tree|intcomma}} </h3>
  <h3>Ordinal : {{ four|ordinal}} </h3>
  <h3>Now : {{ now|naturalday}} </h3>
  <h3>Other Day : {{ otherdate|naturalday}} </h3>
  <h3>Other Day Time : {{ otherdatetime|naturaltime}} </h3>
  <h3>Future : {{ future }} </h3>
  <h3>Past : {{ past }} </h3>

{% endblock %}
Read more on the official Django Documentation https://docs.djangoproject.com/en/dev/ref/contrib/humanize/

Related Post