Django django-admin commands explained

django-admin is a command-line tool provided by the Django framework, and it is the core tool for managing Django projects.

Whether creating a new project, running the development server, or performing database migrations, django-admin is an indispensable tool.

To view all commands provided by django-admin, you can run:

django-admin help

The output will look similar to the following:

Type 'django-admin help <subcommand>' for help on a specific subcommand.

Available subcommands:

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
    flush
    inspectdb
    loaddata
    makemessages
    makemigrations
    migrate
    optimizemigration
    runserver
    sendtestemail
    shell
    showmigrations
    sqlflush
    sqlmigrate
    sqlsequencereset
    squashmigrations
    startapp
    startproject
    test
    testserver

Detailed explanation of commonly used django-admin commands

1. Creating a new project

django-admin startproject 项目名称

This command will create a new Django project in the current directory, including the basic project structure:

  • manage.py:Project management script
  • project_name/:Project root directory
    • __init__.py
    • settings.py:Project settings file
    • urls.py: URL route configuration
    • wsgi.py: WSGI application entry point

2. Creating a new application

Although usually usedmanage.pyto create apps, but you can also use django-admin:

django-admin startapp 应用名称

This will create a new Django application, including:

  • migrations/:Database migration file directory
  • __init__.py
  • admin.py:Admin backend configuration
  • apps.py: Application configuration
  • models.py:Data model definition
  • tests.py: Test code
  • views.py: View function

3. Checking project configuration

django-admin check

This command will check whether your Django project has configuration errors, including:

  • Whether the model definitions are correct
  • Whether the URL configuration is valid
  • Whether the template settings are correct
  • Static file configuration, etc.

4. Database migrations

Django uses a migration system to manage database schema changes:

django-admin makemigrations  # 创建迁移文件
django-admin migrate        # 应用迁移到数据库

5. Creating a superuser

django-admin createsuperuser

This command will guide you through creating a superuser who can access the Django admin site.


Common django-admin commands

CommandFunctionExample
startprojectCreate a new Django projectdjango-admin startproject myproject
startappCreate a new Django appdjango-admin startapp myapp
runserverStart the development serverpython manage.py runserver
makemigrationsGenerate database migration filespython manage.py makemigrations
migrateRun database migrationspython manage.py migrate
createsuperuserCreate administrator accountpython manage.py createsuperuser
shellStart the Django interactive Shellpython manage.py shell
collectstaticCollect static files (for production environment)python manage.py collectstatic
testRun unit testspython manage.py test

django-admin startproject (create project)

Basic syntax:

django-admin startproject <项目名称> [目标目录]

Parameter description:

ParameterFunctionExample
<project_name>Required, project name (will generate a directory with the same name)django-admin startproject mysite
[target directory]Optional, specify the project directorydjango-admin startproject mysite /opt/myproject
--templateUse a custom project templatedjango-admin startproject --template=my_template.zip mysite
--extensionSpecify file extension (e.g.,.py, .txt)django-admin startproject --extension=py,txt mysite
--nameSpecify file name pattern (e.g.,Dockerfile, README.md)django-admin startproject --name=Dockerfile mysite

Example

django-admin startproject mysite  # Create default project
django-admin startproject mysite /opt/code  # Specify Directory
django-admin startproject --template=https://example.com/my_template.zip mysite  # Use remote template

django-admin startapp (create an app)

Basic syntax:

django-admin startapp <应用名称> [目标目录]

Parameter description:

ParameterFunctionExample
<app_name>Required, application name (will generatemodels.py, views.pyetc.)django-admin startapp blog
[target directory]Optional, specify the application directorydjango-admin startapp blog /opt/myapp
--templateUse a custom application templatedjango-admin startapp --template=my_app_template.zip blog

Example

django-admin startapp blog  # Create default app
django-admin startapp blog /opt/myapp  # Specify Directory
django-admin startapp --template=my_template.zip blog  # Use Template

django-admin runserver (start the development server)

Basic syntax:

python manage.py runserver [IP:端口]

Parameter description:

ParameterFunctionExample
[IP:端口]Optional, specify the IP and port to listen on (default127.0.0.1:8000)python manage.py runserver 0.0.0.0:8080
--noreloadDisable auto-reload (used when debugging)python manage.py runserver --noreload
--insecureForce static file serving (non-DEBUG mode)python manage.py runserver --insecure

Example

python manage.py runserver  # Default start (127.0.0.1:8000)
python manage.py runserver 0.0.0.0:8000  # Allow external access
python manage.py runserver 8080  # Only modify port

django-admin migrate (database migration)

Basic syntax:

python manage.py migrate [应用名] [迁移版本]

Parameter description:

ParameterFunctionExample
[app name]Optional, specify the application to migratepython manage.py migrate blog
[migration version]Optional, specify the migration version numberpython manage.py migrate blog 0002
--fakeMark migrations as applied (without actually modifying the database)python manage.py migrate --fake
--fake-initialMarked as executed only if the table already existspython manage.py migrate --fake-initial

Example

python manage.py migrate  # Apply all unapplied migrations
python manage.py migrate blog  # Only migrate the blog app
python manage.py migrate blog 0002  # Migrate to a specific version

Other common commands

CommandFunctionExample
createsuperuserCreate administrator userpython manage.py createsuperuser
shellEnter the Django Shell (with ORM support)python manage.py shell
testRun test casespython manage.py test blog
collectstaticCollect static files (production environment deployment)python manage.py collectstatic
other extensions