Python & Django
Introduction
Python is one of the most popular and versatile programming languages, widely used for web development, automation, data science, AI, and more. In web development, Django and Flask are two of the most widely used frameworks that simplify backend development.
Django is a full-stack web framework that follows the "batteries-included" philosophy, providing built-in tools for authentication, database management, and security.
Flask is a lightweight, minimalistic web framework that provides flexibility, allowing developers to build scalable web applications with custom features.
Why Choose Python for Web Development?
Easy to Learn – Simple and readable syntax.
Huge Ecosystem – Libraries for web, AI, ML, data science, etc.
Fast Development – Django and Flask reduce development time.
Scalability – Used by companies like Instagram, Pinterest, and Reddit.
Security – Django provides built-in security features.
Django – The Full-Featured Web Framework
1️⃣ Installing Django
Install Django using pip:
pip install django
Check if Django is installed:
django-admin --version
2️⃣ Creating a Django Project
django-admin startproject myproject
cd myproject
python manage.py runserver
Visit http://127.0.0.1:8000/ in your browser to see your Django project running!
3️⃣ Django Project Structure
myproject/
│── myproject/ # Main project settings
│── app/ # Application folder (models, views, templates)
│── manage.py # Command-line utility
│── db.sqlite3 # Database
4️⃣ Creating a Django App
python manage.py startapp myapp
Register the app in settings.py
:
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"myapp", # Register your app
]
5️⃣ Django Models – Database Handling
Django uses ORM (Object-Relational Mapping) for database management.
Define a Model in models.py
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=10, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
Apply Migrations to Create the Database Table
python manage.py makemigrations
python manage.py migrate
Create a Superuser for Admin Panel
python manage.py createsuperuser
Visit http://127.0.0.1:8000/admin/ to manage the database using Django’s built-in admin panel.
6️⃣ Django Views & Routing
Define a View in views.py
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, Django! 🎉")
Define a URL Pattern in urls.py
from django.urls import path
from .views import home
urlpatterns = [
path("", home),
]
Visit: http://127.0.0.1:8000/
Output: "Hello, Django!
7️⃣ Django Templates – Rendering HTML
Create a Template (templates/home.html
)
<!DOCTYPE html>
<html>
<head>
<title>Django App</title>
</head>
<body>
<h1>Welcome to Django!</h1>
</body>
</html>
Modify views.py
to Use Templates
from django.shortcuts import render
def home(request):
return render(request, "home.html")
Now your Django app can render HTML pages dynamically!
Last updated
Was this helpful?