इस लेख में, हम देखेंगे कि humanizer क्या है और Django में इसका उपयोग कैसे करें। Humanizer Django में एक फिल्टर है जिसका उपयोग प्रोजेक्ट में मानवीय स्पर्श जोड़ने के लिए किया जाता है। यह Django के सर्वश्रेष्ठ फिल्टर्स में से एक है।
Humanizer का उपयोग संख्याओं को संख्यात्मक आंकड़ों से शब्दों में बदलने, संख्याओं के बीच कॉमा जोड़ने, या संख्याओं को मिलियन या बिलियन में बदलने के लिए किया जाता है। आइए एक उदाहरण लेते हैं और समझते हैं कि इसका उपयोग कैसे करें।
उदाहरण
एक Django प्रोजेक्ट और एक ऐप बनाएं।
प्रोजेक्ट urls कॉन्फ़िगर करें:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include("humanizeproj.urls")),
path('admin/', admin.site.urls),
]
यहां हमने अपने ऐप के लिए urls सेट किए।
ऐप के urls.py में, निम्नलिखित जोड़ें:
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.home, name="home")
]
ऐप के url में, हमने अपना व्यू रेंडर किया।
एक templates फोल्डर बनाएं और इसे 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',
],
},
},
]
यहां हमने टेम्पलेट्स के बेहतर एक्सेस के लिए अपना टेम्पलेट फोल्डर परिभाषित किया।
इसमें home.html जोड़ना न भूलें। अभी इसमें कुछ न लिखें।
settings.py में, अपना ऐप और फिल्टर कॉन्ट्रिब जोड़ें:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# यह जोड़ना आवश्यक है
"humanizeproj", # यह मेरा ऐप नाम है
"django.contrib.humanize"
]
यहां हमने अपना ऐप और मॉड्यूल कॉन्ट्रिब जोड़ा।
views.py में, निम्नलिखित लाइनें जोड़ें:
from django.shortcuts import render
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>
apnumber का उपयोग संख्या को शब्द में बदलने के लिए किया जाता है, intcomma का उपयोग संख्याओं के बीच कॉमा जोड़ने के लिए किया जाता है, और intword का उपयोग पूर्णांकों को मिलियन या बिलियन में बदलने के लिए किया जाता है।
आउटपुट
