← Back to Python Course | Chapter 13: Data Science & Web | Lesson 13 of 14

Python Django परिचय

Django Python से websites बनाने का एक बड़ा, तैयार kit है, जैसे कोई घर जिसमें plumbing और wiring पहले से लगी हो। आप अपने खुद के कमरे जोड़ते हैं, जिन्हें views और pages कहा जाता है।

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

python
# 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

python
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

python
# 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

python
# 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

python
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)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.