LinuxCommandLibrary

django-admin

Manage Django projects

TLDR

Create a new Django project

$ django-admin startproject [project_name]
copy

Create a new app for the current project
$ django-admin startapp [app_name]
copy

Check the current version of Django
$ django-admin --version
copy

Display help for a specific command
$ django-admin help [command]
copy

SYNOPSIS

django-admin [options]
django-admin --version
django-admin --help

PARAMETERS

--version
    Displays the Django version.

--settings=settings.module
    Specifies the settings module to use (e.g., myproject.settings). Used by django-admin to find project-specific configuration.

--pythonpath=path
    Specifies a Python path to add to sys.path.

--traceback
    Shows a full traceback for exceptions during command execution.

--no-color
    Disables colored output, often useful for scripting.

--force-color
    Forces colored output, even if the terminal doesn't support it by default.

--skip-checks
    Skips Django's system checks that run before certain commands.

check
    Runs system checks and validates the project and installed applications.

makemigrations
    Creates new database migrations based on changes detected in models.

migrate
    Applies database migrations to the database, syncing schema with models.

runserver
    Starts a lightweight development server for the project.

startproject
    Creates a new Django project directory structure with default files.

startapp
    Creates a new Django application directory structure within a project.

shell
    Starts an interactive Python shell with the Django project environment loaded.

createsuperuser
    Creates an administrative user for the Django admin site.

collectstatic
    Collects static files (CSS, JavaScript, images) from apps into STATIC_ROOT for deployment.

test
    Runs tests for all installed apps or specific apps.

DESCRIPTION

django-admin is Django's command-line utility for administrative tasks. It provides a wide array of built-in subcommands that allow developers to interact with their Django projects without writing code. This includes creating new projects and applications, running database migrations, managing the database shell, clearing the cache, running development servers, collecting static files, and much more. It's an essential tool for every Django developer, simplifying common development and deployment workflows.

For many subcommands, django-admin needs to know which Django settings module to use, which is typically inferred from the current working directory's project structure or explicitly specified via the --settings option or DJANGO_SETTINGS_MODULE environment variable. While django-admin handles project-wide tasks, a similar command, manage.py, is automatically created inside each Django project. manage.py is effectively a wrapper around django-admin, providing the same subcommands but automatically setting up the DJANGO_SETTINGS_MODULE environment variable to point to your project's settings, making it the preferred way to run commands within a specific Django project.

CAVEATS

Using django-admin directly often requires specifying the --settings option or setting the DJANGO_SETTINGS_MODULE environment variable, especially when not run from the root of a Django project. For project-specific tasks, it's generally recommended to use the manage.py script located in your project's root directory, as it automatically configures the necessary environment variables.

COMMON ENVIRONMENT VARIABLE

The DJANGO_SETTINGS_MODULE environment variable can be set to point to your project's settings file (e.g., export DJANGO_SETTINGS_MODULE=myproject.settings). This allows django-admin to locate your project's configuration without needing the --settings option or being run from a specific directory, which is particularly useful in scripting.

THE MANAGE.PY WRAPPER

The manage.py script, automatically generated in every Django project, is the preferred way to interact with your project's specific configurations. It is essentially a wrapper around django-admin that handles the setup of DJANGO_SETTINGS_MODULE for the current project, making it easier to run project-specific commands like python manage.py runserver.

HISTORY

Django itself was created in 2003 and open-sourced in 2005. The django-admin command-line utility has been a core component since its early days, providing a standardized and comprehensive way to manage Django projects. Its design reflects the 'batteries included' philosophy of Django, offering a wide set of tools directly from the command line, thereby streamlining development and deployment processes for web applications.

SEE ALSO

python(1), pip(1), manage.py (Django project script), sqlite3(1), mysql(1), psql(1)

Copied to clipboard