Django, ek high-level Python web framework hai, jo interactive web applications banane ke liye ek shaktishaali aur efficient tareeka pradaan karta hai. Is step-by-step guide mein hum Django ka istemaal karke ek interactive web application banane ke process ko detail mein samjhayenge.
Step 1: Django Install Karein-
Yeh surakshit karein ki aapke system mein Python install kiya gaya hai. Terminal kholkar neeche diye gaye command ka istemaal karein Django install karne ke liye:
pip install django
Step 2: Django Project Banayein-
Jab Django install ho jaaye, ek naya project banayein neeche diye gaye command se:
django-admin startproject aapkaprojectname
Project directory mein jaayein:
cd aapkaprojectname
Step 3: Django App Banayein-
Django project ek ya ek se adhik apps se bana hota hai. Project ke andar ek nayi app banayein neeche diye gaye command se:
python manage.py startapp aapkaappname
Step 4: Models Ko Define Karein-
Django models database ki structure ko represent karte hain. Apne app directory mein models.py
file kholkar apne models ko define karein.
# aapkaappname/models.py
from django.db import models
class AapkaModel(models.Model):
# Yahan apne model ke fields ko define karein
title = models.CharField(max_length=255)
content = models.TextField()
# Zaroorat ke anusar aur fields jodein
def __str__(self):
return self.title
Step 5: Migrations Banayein-
Model changes ke basis par database migrations generate karein:
python manage.py makemigrations
Migrations apply karke database tables banayein:
python manage.py migrate
Step 6: Views Banayein-
User requests ko handle karne ke liye views ko define karein. Apne app directory mein views.py
file kholkar:
# aapkaappname/views.py
from django.shortcuts import render
from .models import AapkaModel
def index(request):
data = AapkaModel.objects.all()
return render(request, ‘index.html’, {‘data’: data})
Step 7: Templates Banayein-
Dynamic content ko render karne ke liye HTML templates banayein. Apne app directory mein templates
folder banayein aur usme index.html
file add karein:
<!-- aapkaappname/templates/index.html -->
<!DOCTYPE html>
<html lang=“en”>
<head>
<meta charset=“UTF-8”>
<title>Aapki Django App</title>
</head>
<body>
<h1>Aapki Django App</h1>
{% for item in data %}
<p>{{ item.title }} – {{ item.content }}</p>
{% endfor %}
</body>
</html>
Step 8: URLs Configure Karein-
Views ko URLs se map karne ke liye URL patterns define karein. Apne app directory mein urls.py
file kholkar:
# aapkaappname/urls.py
from django.urls import path
from .views import index
urlpatterns = [
path(”, index, name=‘index’),
# Zaroorat ke anusar aur URL patterns add karein
]
In URLs ko project ke urls.py
mein include karein:
# aapkaprojectname/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(”, include(‘aapkaappname.urls’)),
]
Step 9: Development Server Chalayein-
Development server ko shuru karein apni app ko dekhne ke liye:
python manage.py runserver
Apne web browser mein http://127.0.0.1:8000/
par jaayein apni Django app ko dekhne ke liye.
Step 10: Enhance Aur Extend Karein-
Is foundation par aur features jaise ki user authentication, dynamic forms, aur AJAX interactions ko add karein. Django extensive documentation aur ek vibrant community ke saath aata hai jo aapko aage badhne mein madad karega. Advanced topics aur details ke liye Django Documentation explore karein.