π Getting Started with Django
The Story of Your Web Building Adventure
Imagine you want to build a house. You could gather wood, nails, bricks, and figure everything out yourself. OR you could hire a super-smart construction crew that already knows how to build houses perfectly!
Django is that construction crew for websites.
Itβs a free toolkit that helps you build websites fast, safely, and beautifully β without starting from scratch every time.
π€ What is Django?
The Simple Answer
Django is a web framework written in Python.
Think of it like a LEGO kit for websites:
- LEGO gives you pre-made pieces that snap together
- Django gives you pre-made code pieces that work together
- You focus on being creative, not reinventing the wheel!
Real-Life Example
When you use Instagram:
- You post photos β
- You follow friends β
- You see your feed β
Instagram was built with Django! So was Pinterest, Spotifyβs backend, and many more.
# This tiny code can show
# "Hello World" on a website!
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello World!")
Why βDjangoβ?
Named after Django Reinhardt, a famous jazz guitarist. Just like his music was fast and smooth, this framework helps you code fast and smooth!
π§ Django Design Philosophy
Django follows three golden rules that make it special:
1. DRY β Donβt Repeat Yourself
Bad: Writing the same code 10 times in 10 places.
Good: Write it once, use it everywhere!
# Write once, use anywhere!
def get_username(user):
return user.first_name
2. Batteries Included
Django comes with EVERYTHING you need:
- User login system β
- Admin dashboard β
- Database connections β
- Security protection β
No need to hunt for extra parts!
3. Explicit is Better Than Implicit
Django code is clear and readable. No magic tricks hiding whatβs happening.
# Clear and obvious!
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
ποΈ MVT Architecture Pattern
The Restaurant Analogy
Imagine a restaurant:
| Part | Restaurant | Django |
|---|---|---|
| Model | Kitchen (stores recipes) | Database (stores data) |
| View | Waiter (decides what to serve) | Logic (decides what to show) |
| Template | Menu design (how it looks) | HTML (how page looks) |
How It Works Together
graph TD A[π€ User Request] --> B[π URLs] B --> C[π§ View] C --> D[πΎ Model] D --> C C --> E[π¨ Template] E --> F[π± Response to User]
Simple Example
User asks: βShow me the homepageβ
- URL routes to the right View
- View asks Model for data
- Model fetches from database
- View sends data to Template
- Template creates pretty HTML
- User sees the page! π
π» Installing Django
Step 1: Make Sure You Have Python
Open your terminal and type:
python --version
You should see something like Python 3.10.0 or higher.
Step 2: Create a Virtual Environment
Think of this as a clean room for your project:
# Create the room
python -m venv myenv
# Enter the room (Windows)
myenv\Scripts\activate
# Enter the room (Mac/Linux)
source myenv/bin/activate
Step 3: Install Django
Now, bring in the magic:
pip install django
Step 4: Verify Installation
Check if Django is ready:
django-admin --version
You should see something like 5.0 or 4.2.
π Congratulations! Django is installed!
π¬ Creating a Django Project
What is a Project?
A project is your whole website. It contains:
- Settings
- URL configurations
- All your apps
Create Your First Project
django-admin startproject mysite
This creates a folder called mysite with everything inside!
See It Work!
cd mysite
python manage.py runserver
Open your browser and go to: http://127.0.0.1:8000/
π Youβll see: βThe install worked successfully!β
π Django Project Structure
After creating your project, youβll see:
mysite/
βββ manage.py
βββ mysite/
βββ __init__.py
βββ settings.py
βββ urls.py
βββ asgi.py
βββ wsgi.py
What Each File Does
| File | Purpose | Analogy |
|---|---|---|
manage.py |
Command center | Remote control |
settings.py |
Project settings | House rules |
urls.py |
URL routing | Address book |
wsgi.py |
Web server interface | Front door |
asgi.py |
Async server interface | Fast front door |
__init__.py |
Makes it a Python package | ID card |
manage.py β Your Remote Control
# Run the server
python manage.py runserver
# Create database tables
python manage.py migrate
# Create admin user
python manage.py createsuperuser
settings.py β Your House Rules
# Most important settings:
DEBUG = True # Turn off in production!
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
# Your apps go here!
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
urls.py β Your Address Book
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]
π― Quick Summary
graph TD A[π Install Python] --> B[π¦ Create Virtual Env] B --> C[β¬οΈ pip install django] C --> D[π¬ startproject] D --> E[π runserver] E --> F[π Your Site Works!]
Remember These Commands
# Install Django
pip install django
# Create project
django-admin startproject mysite
# Run server
python manage.py runserver
# Create app
python manage.py startapp myapp
π You Did It!
You now understand:
- β What Django is (your web building crew)
- β Djangoβs philosophy (DRY, batteries included)
- β MVT pattern (Model-View-Template)
- β How to install Django
- β How to create a project
- β What each file does
Next step? Start building your first app!
βThe journey of a thousand websites begins with a single
startprojectβ
π Happy coding!