Getting Started with Django#
Complete guide to setting up a Django project from scratch.
🚀 Project Setup#
Create Project#
# Create Django project
django-admin startproject myproject
cd myproject
# Project structure
myproject/
├── manage.py # Django management script
└── myproject/
├── __init__.py
├── settings.py # Project settings
├── urls.py # Root URL configuration
├── wsgi.py # WSGI config for deployment
└── asgi.py # ASGI config for async
Create App#
# Create app within project
python manage.py startapp myapp
# App structure
myapp/
├── __init__.py
├── admin.py # Admin configuration
├── apps.py # App configuration
├── models.py # Database models
├── views.py # View functions/classes
├── urls.py # App URL patterns (create this)
├── forms.py # Forms (create this)
├── tests.py # Tests
└── migrations/ # Database migrations
Register App in INSTALLED_APPS#
# myproject/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Your apps
'myapp', # Add your app here
]
⚙️ Initial Configuration#
Basic Settings#
# myproject/settings.py
# Security
SECRET_KEY = 'your-secret-key-here' # Use environment variable in production
DEBUG = True # False in production
ALLOWED_HOSTS = [] # Add domains in production: ['example.com']
# Database (default SQLite)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Timezone
TIME_ZONE = 'UTC'
USE_TZ = True
# Language
LANGUAGE_CODE = 'en-us'
Environment Variables (Recommended)#
# Install python-decouple
pip install python-decouple
# myproject/settings.py
from decouple import config
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', default=False, cast=bool)
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='', cast=lambda v: [s.strip() for s in v.split(',')])
# .env file (create in project root)
SECRET_KEY=django-insecure-your-secret-key-here
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1
🗄️ Initial Database Setup#
# Create initial migrations
python manage.py migrate
# This creates tables for:
# - auth (users, groups, permissions)
# - sessions
# - admin
# - contenttypes
# - etc.
Create Superuser#
# Create admin user
python manage.py createsuperuser
# Follow prompts:
# Username: admin
# Email: admin@example.com
# Password: (enter secure password)
🏃 Run Development Server#
# Run server
python manage.py runserver
# Run on specific port
python manage.py runserver 8000
# Run on specific IP and port
python manage.py runserver 0.0.0.0:8000
Visit: - http://localhost:8000 - Your site - http://localhost:8000/admin - Admin interface
📁 Project Structure Best Practices#
myproject/
├── manage.py
├── .env # Environment variables (gitignored)
├── requirements.txt # Python dependencies
├── myproject/
│ ├── settings/
│ │ ├── __init__.py
│ │ ├── base.py # Base settings
│ │ ├── development.py # Dev settings
│ │ └── production.py # Prod settings
│ ├── urls.py
│ └── ...
├── myapp/
│ ├── models.py
│ ├── views.py
│ ├── urls.py
│ ├── forms.py
│ ├── admin.py
│ ├── templates/
│ │ └── myapp/
│ │ ├── base.html
│ │ └── index.html
│ └── static/
│ └── myapp/
│ ├── css/
│ ├── js/
│ └── images/
├── static/ # Project-wide static files
├── media/ # User uploads
└── templates/ # Project-wide templates
🔧 Common Commands#
# Create migrations
python manage.py makemigrations
# Apply migrations
python manage.py migrate
# Show migration status
python manage.py showmigrations
# Create superuser
python manage.py createsuperuser
# Start shell
python manage.py shell
# Collect static files (production)
python manage.py collectstatic
# Run tests
python manage.py test
# Check for issues
python manage.py check
📦 Requirements File#
# Generate requirements.txt
pip freeze > requirements.txt
# Install from requirements.txt
pip install -r requirements.txt
Example requirements.txt:
Django==5.0.1
python-decouple==3.8
psycopg2-binary==2.9.9
✅ Next Steps#
- ✅ Create your first app
- ✅ Register app in
INSTALLED_APPS - ✅ Create a simple model (see Models)
- ✅ Create migrations and migrate
- ✅ Create a view and URL (see Views, URLs & Templates)
- ✅ Create a template to display data
Pro Tip: Use python manage.py shell to interact with your models and test queries before writing views!