Django Commands Cheat Sheet

Django Commands Cheat Sheet

Django, a high-level Python web framework, provides a set of management commands that help you perform various tasks during the development and management of a Django project. Here’s a comprehensive list of Django management commands along with brief descriptions of their features:

startproject

Creates a new Django project.

django-admin startproject projectname
startapp

Creates a new Django app within a project.

python manage.py startapp appname
runserver

Starts the development server.

python manage.py runserver
shell

Opens the Python shell with Django environment loaded.

python manage.py shell
makemigrations

Generates new database migration files based on model changes.

python manage.py makemigrations
migrate

Applies database migrations to synchronize the database schema.

python manage.py migrate
createsuperuser

Creates a superuser for the Django admin.

python manage.py createsuperuser
collectstatic

Gathers static files from your apps into a single directory.

python manage.py collectstatic
test

Runs tests for your Django project.

python manage.py test
dbshell

Opens a command-line interface to the database.

python manage.py dbshell
check

Checks for issues in your project without making migrations or touching the database.

python manage.py check
showmigrations

Displays a list of all migrations and their status.

python manage.py showmigrations
shell_plus

Enhanced version of the shell with additional features (requires django-extensions).

python manage.py shell_plus
dumpdata

Outputs the contents of the database as a JSON or XML fixture.

python manage.py dumpdata
loaddata

Loads data from a fixture into the database.

python manage.py loaddata
flush

Resets the database by removing all data.

python manage.py flush
createsuperuser

Creates a superuser for the Django admin.

python manage.py createsuperuser
startapp

Creates a new app within a Django project.

python manage.py startapp appname
runserver

Starts the development server.

python manage.py runserver
runscript

Runs a Python script in the context of a Django project (requires django-extensions).

python manage.py runscript script_name
graph_models

Creates a visual representation of your Django models (requires django-extensions).

python manage.py graph_models -a > models.dot
inspectdb

inspectdb looks at your database tables and prints out Django models. The biggest use case for this is if you have a legacy database that you want to use with Django. The script looks at the database and creates Django models for each table it find.

python manage.py inspectdb

# output of this command will look something like this:
from django.db import models

    class MyTable(models.Model):
        id = models.AutoField(primary_key=True)
        name = models.CharField(max_length=255)
        created_at = models.DateTimeField()
    
        class Meta:
            managed = False
            db_table = 'my_table'

Pro tip: Pass a table name to build models for just that table.

Read more about inspectdb in the Django docs .

dbshell

Opens a command-line interface to the database.

python manage.py dbshell
shell_plus

Enhanced version of the shell with additional features (requires django-extensions).

python manage.py shell_plus
test

Runs tests for your Django project.

python manage.py test
check

Checks for issues in your project without making migrations or touching the database.

python manage.py check
check —deploy

Checks for common issues in a deployment-ready project.

python manage.py check --deploy
show_urls

Displays all URLs defined in the project.

python manage.py show_urls

SUBSCRIBE FOR NEW ARTICLES

@
comments powered by Disqus