Django Project Intro
By understanding Django's project structure, we can organize code more efficiently, follow Django's best practices, and build maintainable Web applications.
In the previous chapter, we learned that when creating a new project using the `django-admin startproject` command, Django automatically generates the following basic structure:
myproject/ββββ manage.py βββ myproject/ βββ __init__.py βββ settings.py βββ urls.py βββ wsgi.py
Use django-admin to create the HelloWorld project:
django-admin startproject HelloWorld
After creation, we can view the project's directory structure:
!(#)
| File/Directory | Detailed Description |
| --- | --- |
| **`manage.py`** | Django project's command-line management tool, which encapsulates the functionality of `django-admin` and automatically sets the `DJANGO_SETTINGS_MODULE` environment variable to point to the current project's configuration. All project management commands are executed through it, such as: - `runserver`: Start the development server - `makemigrations`: Generate database migration files - `shell`: Start Python shell with ORM |
| **`db.sqlite3`** | SQLite database file used by default by Django, automatically generated after the first `migrate` command execution. Suitable for development environment, production environment should use PostgreSQL/MySQL. File location is defined by `DATABASES['default']['NAME']` in `settings.py`. |
| **`__pycache__/`** | Bytecode cache directory generated by Python interpreter, contains `.pyc` files, used to speed up module loading. Should not be committed to version control (should be ignored in `.gitignore`). |
!(#)
* * *
## Core File Analysis
### manage.py
`manage.py` is the entry point for Django project's command-line tool, it provides many useful commands:
## Example
#!/usr/bin/env python
import os
import sys
if __name__ =="__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE","myproject.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# Handle import error
pass
execute_from_command_line(sys.argv)
Common command examples:
* `python manage.py runserver` - Start development server
* `python manage.py migrate` - Apply database migrations
* `python manage.py createsuperuser` - Create admin account
### settings.py
`settings.py` is the configuration file for Django project, contains all important settings:
## Example
# Key configuration options explained:
DEBUG =True# Set to True during development to show detailed errors; must be changed to False in production
ALLOWED_HOSTS =[]# When DEBUG=False, specify allowed domains (e.g., ['example.com'])
INSTALLED_APPS =[
'django.contrib.admin',# Admin site
'django.contrib.auth',# Authentication system
'django.contrib.contenttypes',# Content types framework
'django.contrib.sessions',# Session management
'django.contrib.messages',# Messaging framework
'django.contrib.staticfiles',# Static file management
# Add custom apps: 'myapp.apps.MyAppConfig'
]
DATABASES ={# Database configuration
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',# Use pathlib syntax
# MySQL example:
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME': 'mydb',
# 'USER': 'root',
# 'PASSWORD': 'password',
}
}
STATIC_URL ='/static/'
YouTip