YouTip LogoYouTip

Django Django Admin

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 be similar to: Type 'django-admin help ' for help on a specific subcommand.Available subcommands: 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 Common django-admin Commands ### 1. Create New Project django-admin startproject This command will create a new Django project in the current directory, containing the basic project structure: * `manage.py`: Project management script * `/`: Main project directory * `__init__.py` * `settings.py`: Project settings file * `urls.py`: URL routing configuration * `wsgi.py`: WSGI application entry point ### 2. Create New Application Although `manage.py` is typically used to create applications, you can also use django-admin: django-admin startapp This will create a new Django application, containing: * `migrations/`: Database migration files directory * `__init__.py` * `admin.py`: Admin backend configuration * `apps.py`: Application configuration * `models.py`: Data model definition * `tests.py`: Test code * `views.py`: View functions ### 3. Check Project Configuration django-admin check This command will check if your Django project has any configuration errors, including: * Whether model definitions are correct * Whether URL configuration is valid * Whether template settings are correct * Static file configuration, etc. ### 4. Database Migration Django uses a migration system to manage database schema changes: django-admin makemigrations # Create migration files django-admin migrate # Apply migrations to database ### 5. Create Superuser django-admin createsuperuser This command will guide you through creating a superuser who can access the Django admin backend. * * * ## Common django-admin Commands | Command | Description | Example | | --- | --- | --- | | `startproject` | Create a new Django project | `django-admin startproject myproject` | | `startapp` | Create a new Django application | `django-admin startapp myapp` | | `runserver` | Start development server | `python manage.py runserver` | | `makemigrations` | Generate database migration files | `python manage.py makemigrations` | | `migrate` | Execute database migration | `python manage.py migrate` | | `createsuperuser` | Create admin account | `python manage.py createsuperuser` | | `shell` | Start 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 Project) Basic syntax: django-admin startproject Parameter description: | Parameter | Description | Example | | --- | --- | --- | | `` | Required, project name (will generate a directory with the same name) | `django-admin startproject mysite` | | `` | Optional, specify the directory where the project will be stored | `django-admin startproject mysite /opt/myproject` | | `--template` | Use custom project template | `django-admin startproject --template=my_template.zip mysite` | | `--extension` | Specify file extensions (such as `.py`, `.txt`) | `django-admin startproject --extension=py,txt mysite` | | `--name` | Specify filename patterns (such as `Dockerfile`, `README.md`) | `django-admin startproject --name=Dockerfile mysite` | ## Examples 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 Application) Basic syntax: django-admin startapp Parameter description: | Parameter | Description | Example | | --- | --- | --- | | `` | Required, application name (will generate `models.py`, `views.py`, etc.) | `django-admin startapp blog` | | `` | Optional, specify the directory where the application will be stored | `django-admin startapp blog /opt/myapp` | | `--template` | Use custom application template | `django-admin startapp --template=my_app_template.zip blog` | ## Examples django-admin startapp blog # Create default application django-admin startapp blog /opt/myapp # Specify directory django-admin startapp --template=my_template.zip blog # Use template ### django-admin runserver (Start Development Server) Basic syntax: python manage.py runserver [IP:port] Parameter description: | Parameter | Description | Example | | --- | --- | --- | | `[IP:port]` | Optional, specify the IP and port to listen on (default `127.0.0.1:8000`) | `python manage.py runserver 0.0.0.0:8080` | | `--noreload` | Disable auto-reload (used for debugging) | `python manage.py runserver --noreload` | | `--insecure` | Force static file serving (non-DEBUG mode) | `python manage.py runserver --insecure` | ## Examples 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 # Change port only ### django-admin migrate (Database Migration) Basic syntax: python manage.py migrate Parameter description: | Parameter | Description | Example | | --- | --- | --- | | `` | Optional, specify the application to migrate | `python manage.py migrate blog` | | `` | Optional, specify the migration version number | `python manage.py migrate blog 0002` | | `--fake` | Mark migration as executed (without actually modifying the database) | `python manage.py migrate --fake` | | `--fake-initial` | Only mark as executed when the table already exists | `python manage.py migrate --fake-initial` | ## Examples python manage.py migrate # Execute all unapplied migrations python manage.py migrate blog # Migrate only the blog application python manage.py migrate blog 0002 # Migrate to a specific version ### Other Common Commands | Command | Description | Example | | --- | --- | --- | | `createsuperuser` | Create admin user | `python manage.py createsuperuser` | | `shell` | Enter 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` |
← Java Mock MockitoMysql Python Intro β†’