Python Django परिचय
In this page:
Django का परिचय
Django एक full-featured, batteries-included web framework है जो बड़ी applications के लिए बनाया गया है, और शुरू से ही एक ORM, एक built-in admin interface, और एक authentication system बंडल करके देता है -- Flask की minimalism के मुक़ाबले जानबूझकर लिया गया एक विपरीत रास्ता।
इसे pip install django से install करें।
उदाहरण: Introduction to Django
# pip install django
import django
print(django.get_version())
Django Views परिभाषित करना
एक Django view एक Python function (या class) है जो आने वाली web request स्वीकार करती है और एक web response लौटाती है, सबसे आम तौर पर एक HttpResponse object के रूप में। Views में ही वो असली logic होता है कि किसी particular URL पर जाने पर क्या होता है।
उदाहरण: Defining Django Views
from django.conf import settings
settings.configure() # minimal setup so Django works outside a full project
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, Django!") # views return a response object
response = home(None)
print(response.content)
Django Routing और Paths
Django URL routing को urlpatterns नाम की एक Python list में centralize करता है, जहाँ हर entry path() helper function इस्तेमाल करके एक URL path को किसी view से जोड़ती है।
सारे routes एक ही जगह declare रखना किसी project की पूरी URL structure को एक नज़र में देखना आसान बना देता है।
उदाहरण: Django Routing with Paths
# urls.py
# from django.urls import path
# urlpatterns = [path("home/", home_view)]
print("urlpatterns maps URL paths to view functions")
Django Templates की अवधारणा बनाना
Templates किसी page की HTML structure को उस Python logic से अलग रखते हैं जो उसमें data भरता है, और presentation markup को view code में मिलाने की बजाय अलग .html files में रखते हैं।
Django की template language आपको static HTML में सीधे dynamic values और simple control flow डालने देती है।
उदाहरण: Creating Django Templates Concept
# templates/home.html would contain:
# <h1>{{ title }}</h1>
print("Templates keep HTML separate from view logic")
Django JSONResponse इस्तेमाल करना
JsonResponse API endpoints के लिए बना एक specialized HttpResponse subclass है: इसे एक Python dictionary पास करें और यह अपने-आप उस data को JSON में serialize करके सही Content-Type header सेट कर देता है, जिससे आपको खुद json.dumps() call करने और headers manually सेट करने से बचत होती है।
उदाहरण: Using Django JSONResponse
from django.conf import settings
settings.configure() # minimal setup so Django works outside a full project
from django.http import JsonResponse
response = JsonResponse({"status": "ok"}) # dict is serialized to JSON automatically
print(response.content)
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Python NumPy Introduction
- Python NumPy Arrays
- Python Pandas Introduction
- Python Pandas DataFrame
- Python Matplotlib Basics
- Python Data Visualization
- Python Statistics Module
- Python CSV & Data Analysis
- Python requests Module
- Python JSON & APIs
- Python Web Scraping Basics
- Python Flask Introduction
- Python Django Introduction
- Python MongoDB