Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. It follows the MTV (Model-Template-View) pattern (similar to MVC).
“The web framework for perfectionists with deadlines”
| Feature | Django | Flask |
|---|---|---|
| Type | Full-stack framework | Micro-framework |
| Philosophy | “Batteries included” | Minimalist, flexible |
| Learning Curve | Steeper | Easier to start |
| Admin Panel | Built-in | Manual/third-party |
| ORM | Built-in (Django ORM) | Manual (SQLAlchemy) |
| Authentication | Built-in | Manual/extensions |
| URL Routing | Centralized | Decorator-based |
| Project Structure | Opinionated | Flexible |
| Best For | Large, complex apps | Small to medium apps, APIs |
Flask (Minimal):
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello World!'
if __name__ == '__main__':
app.run()
Django (Structured):
# views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse('Hello World!')
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.hello),
]
Choose Django if:
Choose Flask if:
Based on your current file at blog:
blog/ # PROJECT ROOT
โ
โโโ manage.py # CLI utility (you saw this earlier)
โ
โโโ blog/ # PROJECT CONFIGURATION PACKAGE
โ โโโ __init__.py # Makes it a Python package
โ โโโ settings.py # โ๏ธ Project settings
โ โโโ urls.py # ๐ Main URL routing (you're viewing this)
โ โโโ asgi.py # ASGI deployment config
โ โโโ wsgi.py # WSGI deployment config
โ
โโโ <app_name>/ # DJANGO APP (e.g., posts, comments)
โ โโโ __init__.py
โ โโโ admin.py # Admin interface registration
โ โโโ apps.py # App configuration
โ โโโ models.py # ๐ Database models
โ โโโ views.py # ๐ฏ Business logic
โ โโโ urls.py # App-specific URL routing
โ โโโ tests.py # Unit tests
โ โโโ forms.py # Form definitions (optional)
โ โ
โ โโโ migrations/ # Database migrations
โ โ โโโ __init__.py
โ โ โโโ 0001_initial.py
โ โ
โ โโโ templates/ # HTML templates
โ โ โโโ <app_name>/
โ โ โโโ template.html
โ โ
โ โโโ static/ # CSS, JS, images
โ โโโ <app_name>/
โ โโโ css/
โ โโโ js/
โ โโโ images/
โ
โโโ templates/ # PROJECT-LEVEL TEMPLATES (optional)
โ โโโ base.html
โ
โโโ static/ # PROJECT-LEVEL STATIC FILES (optional)
โ โโโ css/
โ โโโ js/
โ โโโ images/
โ
โโโ media/ # USER-UPLOADED FILES
โ โโโ uploads/
โ
โโโ db.sqlite3 # SQLite database (development)
โโโ requirements.txt # Python dependencies
โโโ .env # Environment variables (not in git)
โโโ .gitignore # Git ignore file
# Command-line utility
python manage.py runserver # Start server
python manage.py makemigrations # Create migrations
python manage.py migrate # Apply migrations
# Project configuration
INSTALLED_APPS = [...] # Registered apps
DATABASES = {...} # Database config
TEMPLATES = [...] # Template settings
STATIC_URL = '/static/' # Static files
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls), # Admin interface
path('', include('posts.urls')), # Include app URLs
path('api/', include('api.urls')), # API endpoints
]
Request Flow:
Browser โ URLs โ Views โ Models/Templates โ Response
โโโโโโโโโโโ
โ URLs โ โ urlpatterns in urls.py
โโโโโโฌโโโโโ
โ
โโโโโโผโโโโโ
โ Views โ โ Functions/classes in views.py
โโโโโโฌโโโโโ
โ
โโโโโโโบ Models (Database)
โ
โโโโโโโบ Templates (HTML)
# 1. Create app
python manage.py startapp posts
# 2. Add to settings.py INSTALLED_APPS
# 'posts',
# 3. Create model in posts/models.py
# 4. Create views in posts/views.py
# 5. Create URLs in posts/urls.py
# 6. Include in main urls.py
Your Current File (urls.py) is the main URL router – it’s the entry point for all HTTP requests to your Django project. Right now it only has the admin route. You’ll add more routes as you create apps!
A Django app is a self-contained module that performs a specific function in your Django project. Think of it as a building block or component of your website.
Key Concept:
blog/ # PROJECT (your entire site)
โโโ manage.py
โโโ blog/ # Project configuration folder
โ โโโ settings.py
โ โโโ urls.py # โ You're viewing this file
โโโ posts/ # APP #1 (handles blog posts)
โ โโโ models.py
โ โโโ views.py
โ โโโ urls.py
โโโ comments/ # APP #2 (handles comments)
โ โโโ models.py
โ โโโ views.py
โ โโโ urls.py
โโโ users/ # APP #3 (handles user profiles)
โโโ models.py
โโโ views.py
โโโ urls.py
# In your terminal at c:\Users\Adam.Huang\Downloads\DE\django\blog\
python manage.py startapp posts
This creates:
posts/
โโโ __init__.py # Makes it a Python package
โโโ admin.py # Register models for admin interface
โโโ apps.py # App configuration
โโโ models.py # Database models (tables)
โโโ views.py # View functions (business logic)
โโโ tests.py # Unit tests
โโโ migrations/ # Database migration files
โโโ __init__.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
from django.shortcuts import render
from .models import Post
def post_list(request):
posts = Post.objects.all()
return render(request, 'posts/post_list.html', {'posts': posts})
from django.urls import path
from . import views
urlpatterns = [
path('', views.post_list, name='post_list'),
]
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'posts', # โ Add your app here
]
from django.contrib import admin
from django.urls import path, include # โ Add include
urlpatterns = [
path('admin/', admin.site.urls),
path('posts/', include('posts.urls')), # โ Include app URLs
]
Now URLs like http://127.0.0.1:8000/posts/ will route to your posts app!
Your project already has these apps (check settings.py):
โ DO:
โ DON’T:
Next Steps: Create your first app with python manage.py startapp posts and start building your blog!