Djando and Django-extension Commands
Core Django Commands
These commands are provided by the Django framework itself.
1. Project Management Commands
startproject <project_name>: Creates a new Django project with the specified name.django-admin startproject my_project- Structure:
manage.py: Entry point for project commands.- Project folder with settings, URLs, and ASGI/WSGI files.
- Structure:
2. App Management Commands
startapp <app_name>: Creates a new Django app within your project.python manage.py startapp my_app
3. Database Management Commands
migrate: Applies database migrations for installed apps.python manage.py migratemakemigrations: Creates migration files for model changes.python manage.py makemigrationssqlmigrate <app_name> <migration_number>: Displays the SQL commands Django would run for a migration.python manage.py sqlmigrate my_app 0001
4. Server and Debugging
runserver: Starts the development server.python manage.py runserver- Default: Runs on
localhost:8000. - Specify IP/port:
python manage.py runserver 0.0.0.0:8080.
- Default: Runs on
check: Validates the project for issues.python manage.py check
5. User Management
createsuperuser: Creates an admin user for the project.python manage.py createsuperuserchangepassword <username>: Changes the password for a user.python manage.py changepassword admin
6. Testing and Debugging
test: Runs all test cases in the project.python manage.py test
7. Other Commands
showmigrations: Displays migrations and their status.python manage.py showmigrations
Django Extensions Commands
Django Extensions adds powerful custom commands to extend functionality beyond the core Django commands.
1. Shell and Debugging
shell_plus: An enhanced shell command that auto-imports models and settings.python manage.py shell_plusshell_plus --notebook: Integrates with Jupyter Notebook for a better debugging interface.
2. Model Visualization
graph_models: Generates a diagram of your models and relationships.
python manage.py graph_models my_app -o models.png
python manage.py graph_models budget --dot -o dot.txt- Requires Graphviz.
3. URL Management
show_urls: Lists all registered URLs in your project.python manage.py show_urls
4. Profiler and Performance
runprofileserver: Profiles your project to detect performance bottlenecks.python manage.py runprofileserver
5. Jobs and Background Tasks
create_jobs: Creates job templates for background tasks.python manage.py create_jobs my_job
6. Database Commands
sqlcreate: Outputs SQL for creating a database.python manage.py sqlcreate
7. Random Data Generation
generate_password: Generates a random secure password.python manage.py generate_password
8. Command Enhancements
print_settings: Displays project settings for debugging.python manage.py print_settings
9. Logging and Testing
test_with_coverage: Runs tests with coverage analysis.python manage.py test_with_coverage
Comparison
| Category | Django Commands | Django Extensions Commands |
|---|---|---|
| Debugging | runserver, check |
shell_plus, runprofileserver |
| Database | migrate, makemigrations |
sqlcreate, graph_models |
| Model Management | showmigrations, sqlmigrate |
graph_models |
| Performance | Not Available | runprofileserver |
| URL Insights | Not Available | show_urls |
| Testing | test |
test_with_coverage |
How to Explore All Commands
To list all available commands in your project:
python manage.py helpFor Django Extensions commands:
python manage.py help | grep django_extensionsTips for Effective Use
- Use
shell_plusfor fast prototyping and debugging. - Leverage
graph_modelsto understand model relationships visually. - Combine
runprofileserverwith test cases for performance tuning. - Use
show_urlsfor insights into the URL structure.
Both Django and Django Extensions commands are designed to make development efficient and organized!