Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! π
Django Project Structure Explained
Understanding Django's project structure is key to working effectively. Every file has a specific purpose - from settings.py (configuration) to urls.py (routing) to manage.py (CLI tool). Let's break down each file.
Project Structure Breakdown
- manage.py - CLI tool, runs commands (runserver, migrate, createsuperuser)
- settings.py - Database config, installed apps, middleware, templates, static files
- urls.py - Maps URL patterns to views (the URL router)
- wsgi.py - Web Server Gateway Interface entry point (production)
- asgi.py - Asynchronous Server Gateway Interface (async/WebSocket support)
- __init__.py - Marks the directory as a Python package
- Each app has its own models.py, views.py, urls.py, admin.py, tests.py
settings.py Key Settings
# mysite/settings.py - Key configurations
# DEBUG = True # Set to False in production!
# INSTALLED_APPS - All active apps in this project
# INSTALLED_APPS = [
# 'django.contrib.admin', # Admin panel
# 'django.contrib.auth', # Authentication
# 'django.contrib.contenttypes',# Content type framework
# 'django.contrib.sessions', # Session management
# 'django.contrib.messages', # Messaging framework
# 'django.contrib.staticfiles', # Static file serving
# # Add your apps here:
# # 'myapp',
# ]
# DATABASES - Database configuration
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }
# STATIC_URL = '/static/'
# MEDIA_URL = '/media/'Tip
Tip
Break your first project into tiny steps: model β admin β views β templates β URLs. Get each working before moving on.
QuerySets are LAZY - no DB hit until evaluated.
Common Mistake
Warning
Skipping migrations after model changes. Always run makemigrations and migrate after any model modification.
Practice Task
Note
(1) Create a Post model with title, content, created_at. (2) Register it in admin. (3) Create views and templates to list and detail posts.
Quick Quiz
Key Takeaways
- Understanding Django's project structure is key to working effectively.
- manage.py - CLI tool, runs commands (runserver, migrate, createsuperuser)
- settings.py - Database config, installed apps, middleware, templates, static files
- urls.py - Maps URL patterns to views (the URL router)