Skip to content

Latest commit

 

History

History
200 lines (135 loc) · 3.96 KB

File metadata and controls

200 lines (135 loc) · 3.96 KB

Django vs Flask: Key Differences (E-commerce & ML Focus)

Core Philosophy

Aspect Django Flask
Structure Monolithic (strict folders, settings.py) Micro (single app.py possible)
Defaults Everything included (ORM, Admin, Forms, Auth) Nothing included (add what you need)
Learning Steeper (many concepts) Faster (routes + templates)
Speed Slower startup (heavy framework) Lightning fast (minimal overhead)

E-commerce: Django Wins

✅ Built-in User Auth (login/register/forgot password) ✅ Admin Panel (manage products/orders instantly) ✅ ORM (Product → Order → OrderItem relationships) ✅ Forms (validation, CSRF protection) ✅ Sessions + Cart management ✅ File uploads (product images) ✅ Payments (Razorpay integration ready)

Django = 1 week → Full E-commerce store

Flask = 3 weeks → Same features (build everything)

Example: Razorpay integration (your hackfest) = 20 lines in Django, 50+ lines in Flask.razorpay


ML: Flask Wins

✅ Serve ML models via API (TensorFlow/PyTorch) ✅ Fast prototyping (Jupyter → Flask API → Done) ✅ Low overhead (no Django ORM bloat) ✅ Easy Docker deployment ✅ Serve predictions in <100ms


Decision Matrix

Use Case Winner Why
E-commerce Django ✅ Auth + Admin + ORM = 70% code saved
ML APIs Flask ✅ Minimal overhead, fast prototyping
REST APIs Flask ✅ Lightweight, no Django bloat
Admin-heavy Django ✅ Built-in admin = magic
Prototypes Flask ✅ app.py → Done
Complex apps Django ✅ Scales better (Instagram uses Django)

Real-World Examples

E-commerce Giants: ✅ Instagram (Django) ✅ Pinterest (Django) ✅ Disqus (Django)

ML Companies: ✅ FastAPI (Flask-like) for ML ✅ Many ML startups use Flask microservices


Code Comparison (Same Feature)

Flask (URL Shortener - 25 lines):

python

@app.route('/shorten', methods=['POST']) def shorten(): url = request.json['url'] code = generate_short_code() db[code] = url # Redis return jsonify({'short_url': f"http://short/{code}"})

Django (Same feature - 15 lines):

python

`def shorten(request): url = request.POST['url'] short_url = ShortURL.objects.create(original_url=url) return JsonResponse({'short_url': short_url.short_code})

Django wins on features, Flask wins on simplicity.

Hello World in Flask (Using Template)

1️⃣ Install Flask

pip install flask

2️⃣ Project Structure

“Flask is simple, but structure still matters.”

flask_app/
│── app.py
└── templates/
    └── hello.html

If templates/ folder name is wrong, Flask will NOT find the file.
This is a common beginner mistake.


3️⃣ Create Flask App

app.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("hello.html")

if __name__ == "__main__":
    app.run(debug=True)

👉 What you say orally:

  • @app.route("/") → URL

  • home() → function that runs

  • render_template() → sends HTML to browser


4️⃣ Create Template File

templates/hello.html

<!DOCTYPE html>
<html>
<head>
    <title>Flask App</title>
</head>
<body>
    <h1>Hello World from Flask Template</h1>
</body>
</html>

“HTML stays in template, Python stays in app.py
Flask separates logic and UI.”


5️⃣ Run the App

python app.py

Open browser:

http://127.0.0.1:5000/

If this page loads → Flask is working correctly.


6️⃣ (Optional) Pass Data to Template (Very Small Upgrade)

app.py

@app.route("/")
def home():
    return render_template("hello.html", name="Student")

hello.html

<h1>Hello {{ name }} 👋</h1>

“Flask can send data from Python to HTML.”