Getting Started with Django
Install Django, create your first project and app, configure settings, and run the development server.
Django is a “batteries-included” web framework — it ships with an ORM, admin panel, authentication, form handling, and more out of the box.
Installation
python -m venv .venv
source .venv/bin/activate
pip install django
django-admin --version
Create a Project
django-admin startproject mysite
cd mysite
python manage.py startapp blog
Project structure:
mysite/
├── manage.py
├── mysite/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── blog/
├── models.py
├── views.py
├── urls.py
└── admin.py
Register Your App
Add 'blog' to INSTALLED_APPS in mysite/settings.py:
INSTALLED_APPS = [
...
'blog',
]
Your First View
# blog/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, Django!")
Wire it up in URLs:
# blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
# mysite/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
Run the Development Server
python manage.py runserver
Visit http://127.0.0.1:8000/blog/ — you should see “Hello, Django!”
Django Admin
Create a superuser to access the built-in admin panel:
python manage.py createsuperuser
Visit http://127.0.0.1:8000/admin/ and log in.
Key Concepts
- Project — the overall site configuration (
mysite/) - App — a modular component (
blog/) that does one thing well - manage.py — CLI tool for running commands
- settings.py — central configuration (database, apps, middleware)
Next: Models & Database — define data structures and interact with the database.
Settings Overview
Key sections in mysite/settings.py:
# Debug — NEVER leave True in production
DEBUG = True
# Hosts allowed to serve the site
ALLOWED_HOSTS = ["127.0.0.1", "localhost"]
# Database — SQLite by default (good for development)
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
# Static files (CSS, JS, images)
STATIC_URL = "static/"
Use environment variables for secrets in production:
import os
SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY", "dev-only-key")
DEBUG = os.environ.get("DEBUG", "False") == "True"
Templates — Dynamic HTML
Replace plain HttpResponse with templates:
# blog/views.py
from django.shortcuts import render
def index(request):
posts = [
{"title": "First Post", "body": "Hello, world!"},
{"title": "Second Post", "body": "Django is great."},
]
return render(request, "blog/index.html", {"posts": posts})
Create blog/templates/blog/index.html:
<!DOCTYPE html>
<html>
<head><title>My Blog</title></head>
<body>
<h1>Blog Posts</h1>
{% for post in posts %}
<article>
<h2>{{ post.title }}</h2>
<p>{{ post.body }}</p>
</article>
{% endfor %}
</body>
</html>
Django auto-discovers templates in each app’s templates/ folder.
Django Request Flow
Browser → urls.py → view function → (model/query) → template → HTML response
Every request passes through middleware (security, sessions, auth) before reaching your view.
Common manage.py Commands
| Command | Purpose |
|---|---|
runserver |
Start development server |
migrate |
Apply database migrations |
makemigrations |
Generate migrations from model changes |
createsuperuser |
Create admin login |
shell |
Interactive Python shell with Django loaded |
test |
Run test suite |
python manage.py shell
>>> from django.contrib.auth.models import User
>>> User.objects.count()
Project vs App — When to Split
| Create a new app when… | Keep in same app when… |
|---|---|
| Feature is logically separate (blog, shop, users) | It’s a few related views |
| You might reuse it across projects | It’s tightly coupled to one feature |
| Team members work on it independently | Prototype / learning project |
Start with one app; split when complexity grows.
Next Steps
- Models & Database — define
PostandCategorymodels - Views & Templates — class-based views and forms
- Django Blog Project — full application walkthrough