इस लेख में, हम देखेंगे कि मानवीकरण क्या है और Django में इसका उपयोग कैसे करें। ह्यूमनाइज़र Django में एक फ़िल्टर है जिसका उपयोग किसी प्रोजेक्ट में मानवीय स्पर्श जोड़ने के लिए किया जाता है। यह Django के सबसे अच्छे फिल्टर में से एक है।
ह्यूमनाइज़र का उपयोग संख्याओं को संख्यात्मक अंकों से शब्दों में बदलने, या संख्याओं के बीच अल्पविराम जोड़ने या संख्याओं को मिलियन या बिलियन में बदलने के लिए किया जाता है। आइए एक उदाहरण लेते हैं और समझते हैं कि इसका उपयोग कैसे करना है।
उदाहरण
एक Django प्रोजेक्ट और एक ऐप बनाएं।
प्रोजेक्ट url कॉन्फ़िगर करें -
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('',include("humanizeproj.urls")),
path('admin/', admin.site.urls),
] यहां हम अपने यूआरएल . सेट अप करते हैं हमारे ऐप के लिए।
ऐप के urls.py . में , निम्नलिखित जोड़ें -
from django.urls import path,include
from . import views
urlpatterns = [
path('', views.home, name="home")
] ऐप के url में, हमने अपना विचार प्रस्तुत किया।
एक टेम्पलेट बनाएं फ़ोल्डर और इसे settings.py . में कॉन्फ़िगर करें -
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTem plates',
'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',
],
},
},
] यहां हमने टेम्प्लेट की बेहतर पहुंच के लिए अपने टेम्प्लेट फ़ोल्डर को परिभाषित किया है।
साथ ही, home.html add जोड़ना न भूलें इस में। इसके अंदर अभी के लिए कुछ भी न लिखें।
Settings.py में, अपना ऐप जोड़ें और योगदान फ़िल्टर करें -
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # This is needed to add "humanizeproj", # this is my app name "django.contrib.humanize" ]
यहां हमने अपना ऐप और मॉड्यूल कंट्रीब जोड़ा।
views.py, . में निम्नलिखित पंक्तियाँ जोड़ें -
from django.shortcuts import render # Create your views here. def home(request): return render(request,"home.html")
यहां हमने अपना फ़्रंटएंड प्रस्तुत किया है जो home.html. . है
home.html . में , जोड़ें -
<!DOCTYPE html>
<html>
<head>
<title>Tut</title>
</head>
<body>
{% load humanize %}
<h1>Numbers(apnumber function) :</h1>
{{"1" | apnumber}}<br>
{{"2" | apnumber}}<br>
{{"10" | apnumber}}<br>
<h1>integer comma:</h1>
{{"1000" | intcomma}}<br>
{{"200000" | intcomma}}<br>
{{"10000000" | intcomma}}<br>
<h1>integer word:</h1>
{{"1000000000" | intword}}<br>
{{"20000000000" | intword}}<br>
{{"10000000" | intword}}<br>
</body>
</html> अपनंबर संख्या को शब्द में बदलने के लिए प्रयोग किया जाता है, intcoma संख्याओं के बीच अल्पविराम जोड़ने के लिए प्रयोग किया जाता है, और इंटवर्ड पूर्णांकों को मिलियन या अरब में बदलने के लिए प्रयोग किया जाता है।
आउटपुट
