Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Django django-admin Commands

Django django-admin Commands in Detail

django-admin is a command-line tool provided by the Django framework; 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, run:



django-admin help

The output is similar to the following:

Type 'django-admin help ' 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


Common django-admin commands in detail

1. Create a new project



django-admin startproject 项目名称

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

- `__init__.py` - `settings.py`: project settings file - `urls.py`: URL routing configuration - `wsgi.py`: WSGI application entry

2. Create a new app

Although it is usually created using manage.py to create an app, you can also use django-admin:



django-admin startapp 应用名称

This creates a new Django app, including:

3. Check project configuration



django-admin check

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

4. Database migration

Django uses a migration system to manage database schema changes:



django-admin makemigrations  # 创建迁移文件

django-admin migrate        # 应用迁移到数据库

5. Create a superuser



django-admin createsuperuser

This command guides you to create a superuser who can access the Django admin backend.


Common django-admin commands

Command Purpose Example
startproject Create a new Django project django-admin startproject myproject
startapp Create a new Django app django-admin startapp myapp
runserver Start the Dev Server python manage.py runserver
makemigrations Generate database migration files python manage.py makemigrations
migrate Execute database migrations python manage.py migrate
createsuperuser Create an administrator account python manage.py createsuperuser
shell Start the Django interactive Shell python manage.py shell
collectstatic Collect static files (for production) python manage.py collectstatic
test Run unit tests python manage.py test

django-admin startproject (create a project)

Basic syntax:



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

Parameter description:

Parameters Purpose Example
<项目名称> Required, project name (a directory with the same name will be generated) django-admin startproject mysite
[目标目录] Optional, specify the project storage directory django-admin startproject mysite /opt/myproject
--template Use a custom project template django-admin startproject --template=my_template.zip mysite
--extension Specify the file extension (e.g., .py, .txt) django-admin startproject --extension=py,txt mysite
--name Specify the file name pattern (e.g., Dockerfile, README.md) django-admin startproject --name=Dockerfile mysite

Example

django-admin startproject mysite  

Create a default project

django-admin startproject mysite

/

opt

/

code  

Specify directory

django-admin startproject

--template

=https:

//

example.com

/

my_template.zip mysite  

Use a remote template

django-admin startapp (create an app)

Basic syntax:



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

Parameter description:

Parameters Purpose Example
<应用名称> Required, app name (will generate models.py, views.py etc.) django-admin startapp blog
[目标目录] Optional, specify the app storage directory django-admin startapp blog /opt/myapp
--template Use a custom app template django-admin startapp --template=my_app_template.zip blog

Example

django-admin startapp blog  

Create a default app

django-admin startapp blog

/

opt

/

myapp  

Specify directory

django-admin startapp

--template

=my_template.zip blog  

Use a template

django-admin runserver (start the development server)

Basic syntax:



python manage.py runserver [IP:端口]

Parameter description:

Parameters Purpose Example
[IP:端口] Optional, specify the listening IP and port (default 127.0.0.1:8000) python manage.py runserver 0.0.0.0:8080
--noreload Disable auto-reload (used when debugging) python manage.py runserver --noreload
--insecure Force static file serving (non-DEBUG mode) python manage.py runserver --insecure

Example

python manage.py runserver  

Default startup (127.0.0.1:8000)

python manage.py runserver 0.0.0.0:

8000

 

Allow external access

python manage.py runserver

8080

 

Only change the port

django-admin migrate (database migration)

Basic syntax:



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

Parameter description:

Parameters Purpose Example
[应用名] Optional, specify the app to migrate python manage.py migrate blog
[迁移版本] Optional, specify the migration version number python manage.py migrate blog 0002
--fake Mark a migration as applied (without actually modifying the database) python manage.py migrate --fake
--fake-initial Mark as applied only if the table already exists python manage.py migrate --fake-initial

Example

python manage.py migrate  

Execute 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

Command Purpose Example
createsuperuser Create an administrator user python manage.py createsuperuser
shell Enter the Django Shell (with ORM support) python manage.py shell
test Run test cases python manage.py test blog
collectstatic Collect static files (production deployment) python manage.py collectstatic