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.

The Ultimate Django Commands Cheat Sheet — Everything You Need! 🚀
Photo by hannah joshua on Unsplash

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.

Photo by Marten Bjork on Unsplash

1. Create a New Django Project

This command creates a new Django project with the specified name.

django-admin startproject project_name

2. Run the Development Server

This command starts Django’s built-in web server for local development.

python manage.py runserver

This 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 8001

3. 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_name

Database & Migrations

Migrations help manage changes in database schema over time. The following commands help you to manage your database.

Photo by Taylor Vick on Unsplash

4. Apply Migrations

This command is used to applies all pending migrations to the database.

python manage.py migrate

5. Create Migrations for Changes

This command is used to generates migration files based on model changes.

python manage.py makemigrations

6. 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_number

Example:

python manage.py sqlmigrate blog 0001

7. Check for Pending Migrations

This command lists all migrations and their statuses.

python manage.py showmigrations

User & Authentication

Django provides built-in authentication and user management. The following commands help you to manage your authentication and user management.

Photo by Franck on Unsplash

8. Create a Superuser

This command is used to creates an admin user for accessing the Django Admin Panel.

python manage.py createsuperuser

You’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 username

You’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 shell

Then, 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.

Photo by Taylor Vick on Unsplash

11. Open the Database Shell

This command opens the database shell to run SQL queries directly.

python manage.py dbshell

12. Reset the Database (Dangerous!)

This command deletes all data from the database but keeps migration history.

python manage.py flush

Warning: This will remove all data but retain the table structure.

Debugging & Shell

Django provides interactive tools for debugging and testing.

Photo by Sigmund on Unsplash

13. Open the Django Shell

This command launches a Python shell to interact with Django models and objects.

python manage.py shell

14. 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_plus

15. 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_urls

Static Files & Media

Django handles static files (CSS, JavaScript, images) separately from dynamic content. The following commands help you manage them effectively.

Photo by Savannah Wakefield on Unsplash

16. Collect Static Files (for Production)

This command collects all static files into the STATIC_ROOT directory for deployment.

python manage.py collectstatic

Make 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.

Photo by Nguyen Dang Hoang Nhu on Unsplash

17. Run All Tests

This command runs all test cases in the project.

python manage.py test

18. Run Tests for a Specific App

This command runs tests for a specific Django app.

python manage.py test app_name

Performance & Optimization

Optimize and debug Django projects with these commands.

Photo by Austin Distel on Unsplash

19. Check for Common Issues

This command detects problems in settings, models, and configurations.

python manage.py check

20. Monitor SQL Queries in Debug Mode

This command logs all executed SQL queries in the database shell.

python manage.py dbshell

For SQLite:

sqlite> .log stderr

Django Custom Commands

Django offers built-in help for discovering available commands.

Photo by Toa Heftiba on Unsplash

21. List All Available Django Commands

This command displays all the available management commands.

python manage.py help

22. 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 migrate

Conclusion

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! 🚀