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 scriptproject_name/:Project root directory__init__.pysettings.py:Project settings fileurls.py: URL route configurationwsgi.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__.pyadmin.py:Admin backend configurationapps.py: Application configurationmodels.py:Data model definitiontests.py: Test codeviews.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
| Command | Function | Example |
|---|---|---|
startproject | Create a new Django project | django-admin startproject myproject |
startapp | Create a new Django app | django-admin startapp myapp |
runserver | Start the development server | python manage.py runserver |
makemigrations | Generate database migration files | python manage.py makemigrations |
migrate | Run database migrations | python manage.py migrate |
createsuperuser | Create administrator account | python manage.py createsuperuser |
shell | Start the Django interactive Shell | python manage.py shell |
collectstatic | Collect static files (for production environment) | python manage.py collectstatic |
test | Run unit tests | python manage.py test |
django-admin startproject (create project)
Basic syntax:
django-admin startproject <项目名称> [目标目录]
Parameter description:
| Parameter | Function | Example |
|---|---|---|
<project_name> | Required, project name (will generate a directory with the same name) | django-admin startproject mysite |
[target directory] | Optional, specify the project directory | django-admin startproject mysite /opt/myproject |
--template | Use a custom project template | django-admin startproject --template=my_template.zip mysite |
--extension | Specify file extension (e.g.,.py, .txt) | django-admin startproject --extension=py,txt mysite |
--name | Specify file name pattern (e.g.,Dockerfile, README.md) | django-admin startproject --name=Dockerfile mysite |
Example
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:
| Parameter | Function | Example |
|---|---|---|
<app_name> | Required, application name (will generatemodels.py, views.pyetc.) | django-admin startapp blog |
[target directory] | Optional, specify the application directory | django-admin startapp blog /opt/myapp |
--template | Use a custom application template | django-admin startapp --template=my_app_template.zip blog |
Example
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:
| Parameter | Function | Example |
|---|---|---|
[IP:端口] | Optional, specify the IP and port to listen on (default127.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 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:
| Parameter | Function | Example |
|---|---|---|
[app name] | Optional, specify the application to migrate | python manage.py migrate blog |
[migration version] | Optional, specify the migration version number | python manage.py migrate blog 0002 |
--fake | Mark migrations as applied (without actually modifying the database) | python manage.py migrate --fake |
--fake-initial | Marked as executed only if the table already exists | python manage.py migrate --fake-initial |
Example
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 | Function | Example |
|---|---|---|
createsuperuser | Create 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 environment deployment) | python manage.py collectstatic |