> For the complete documentation index, see [llms.txt](https://www.anandisheladiya.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.anandisheladiya.com/skills-and-experience/backend/python-and-django.md).

# 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**:

```bash
pip install django
```

Check if Django is installed:

```bash
django-admin --version

```

2️⃣ **Creating a Django Project**

```bash
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**

```csharp
myproject/
│── myproject/   # Main project settings
│── app/         # Application folder (models, views, templates)
│── manage.py    # Command-line utility
│── db.sqlite3   # Database

```

4️⃣ Creating a Django App

```bash
python manage.py startapp myapp

```

Register the app in `settings.py`:

```python
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`**

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

```bash
python manage.py makemigrations
python manage.py migrate

```

Create a Superuser for Admin Panel

```bash
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`

```python
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello, Django! 🎉")

```

Define a URL Pattern in `urls.py`

```python
from django.urls import path
from .views import home

urlpatterns = [
    path("", home),
]

```

**Visit:** \*\*<http://127.0.0.1:8000/**\\>
**Output:** `"Hello, Django!`&#x20;

### **7️⃣ Django Templates – Rendering HTML**

**Create a Template (`templates/home.html`)**

```html
<!DOCTYPE html>
<html>
<head>
    <title>Django App</title>
</head>
<body>
    <h1>Welcome to Django!</h1>
</body>
</html>

```

Modify `views.py` to Use Templates

```python
from django.shortcuts import render

def home(request):
    return render(request, "home.html")

```

Now your Django app can render HTML pages dynamically!&#x20;
