The Ultimate Django Commands Cheat Sheet — Everything You Need! 🚀
--
Django’s command-line interface (CLI) is packed with powerful commands that make development faster and easier.
In this guide we will dive into comprehensive cheat sheet that will cover essential Django commands for managing projects, apps, databases, migrations, and more!
Project Management
Django projects consist of multiple apps. The following commands help you create and manage them effectively.
1. Create a New Django Project
This command creates a new Django project with the specified name.
django-admin startproject project_name2. Run the Development Server
This command starts Django’s built-in web server for local development.
python manage.py runserverThis will run on default address: 127.0.0.1:8000
You can even run on a custom port by adding the <port number>:
python manage.py runserver 80013. Create a New Django App
This command creates a new app inside a Django project. Each app handles a specific functionality.
python manage.py startapp app_nameDatabase & Migrations
Migrations help manage changes in database schema over time. The following commands help you to manage your database.
4. Apply Migrations
This command is used to applies all pending migrations to the database.
python manage.py migrate5. Create Migrations for Changes
This command is used to generates migration files based on model changes.
python manage.py makemigrations6. Show SQL Queries of Migrations
This command is used to displays the raw SQL queries for a specific migration.
python manage.py sqlmigrate app_name migration_numberExample:
python manage.py sqlmigrate blog 00017. Check for Pending Migrations
This command lists all migrations and their statuses.
python manage.py showmigrationsUser & Authentication
Django provides built-in authentication and user management. The following commands help you to manage your authentication and user management.
8. Create a Superuser
This command is used to creates an admin user for accessing the Django Admin Panel.
python manage.py createsuperuserYou’ll be prompted to enter a username, email, and password.
9. Change a User’s Password
This command is used to updates the password of an existing user.
python manage.py changepassword usernameYou’ll be prompted to enter a new password.
10. List All Users in the Database
To Displays all registered users in the database.
python manage.py shellThen, in the Python shell:
from django.contrib.auth.models import User
User.objects.all()Database Management
These commands help manage and interact with the database.
11. Open the Database Shell
This command opens the database shell to run SQL queries directly.
python manage.py dbshell12. Reset the Database (Dangerous!)
This command deletes all data from the database but keeps migration history.
python manage.py flushWarning: This will remove all data but retain the table structure.
Debugging & Shell
Django provides interactive tools for debugging and testing.
13. Open the Django Shell
This command launches a Python shell to interact with Django models and objects.
python manage.py shell14. Run the Shell with IPython (Better Debugging)
This command provides an enhanced shell with better debugging features (requires django-extensions package).
python manage.py shell_plus15. Display All URLs in Your Project
This commadn lists all URL patterns defined in the Django project (requires django-extensions package).
python manage.py show_urlsStatic Files & Media
Django handles static files (CSS, JavaScript, images) separately from dynamic content. The following commands help you manage them effectively.
16. Collect Static Files (for Production)
This command collects all static files into the STATIC_ROOT directory for deployment.
python manage.py collectstaticMake sure your static settings are configured correctly.
Running Tests
Django provides built-in testing capabilities to ensure app functionality. The following command helps to manage the test effectively.
17. Run All Tests
This command runs all test cases in the project.
python manage.py test18. Run Tests for a Specific App
This command runs tests for a specific Django app.
python manage.py test app_namePerformance & Optimization
Optimize and debug Django projects with these commands.
19. Check for Common Issues
This command detects problems in settings, models, and configurations.
python manage.py check20. Monitor SQL Queries in Debug Mode
This command logs all executed SQL queries in the database shell.
python manage.py dbshellFor SQLite:
sqlite> .log stderrDjango Custom Commands
Django offers built-in help for discovering available commands.
21. List All Available Django Commands
This command displays all the available management commands.
python manage.py help22. Get Help on a Specific Command
This command shows detailed help for a specific command.
python manage.py help <command>Example:
python manage.py help migrateConclusion
These Django commands will accelerate your development workflow and help you manage projects efficiently. Bookmark this cheat sheet and keep it handy for your Django projects!
👉 Did I miss any essential commands? Let me know in the comments! 🚀
