5 Django Commands Every Developer Should Know
In this brief blog, I outline what I consider the must-know commands in django especially if you're a beginner
django-admin startproject
This command is used to create a new Django project. It sets up the basic directory structure and configuration files for a Django project. For example, running django-admin startproject mylibrary
will create a new project named "mylibrary" with the following directory structure:
mylibrary/
manage.py
mylibrary/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
python manage.py runserver
This command starts the development server, allowing you to run your Django application locally. It launches a web server on your local machine, enabling you to view and test your application in a web browser. When you run the python manage.py runserver
command in your project directory, you should see the following output on the command line:
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
July 06, 2023 - 15:50:53
Django version 4.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
python manage.py makemigrations
python manage.py migrate
These two commands are used to manage database migrations in Django. When you make changes to your models (such as adding a new field or table), running makemigrations
generates the necessary migration files. Then, running migrate
applies those migrations to the database, updating its schema.
python manage.py createsuperuser
This command allows you to create a superuser account to access the Django admin interface. The admin interface provides a convenient way to manage your application's data. Running python manage.py createsuperuser
prompts you to enter a username, email (this is optional), and password for the superuser. After this is created, you can now access the admin interface in your web browser.
python manage.py startapp
This command creates a new Django app within your project. Django apps are modular components that encapsulate specific functionalities. Running python manage.py startapp mybooks
generates the necessary files and folders to get started with a new app named "mybooks." This is what the directory structure should look like:
mybooks/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
These are just a few essential commands in Django. There are many more commands available to perform various tasks, such as running tests, managing static files, and creating custom management commands. Happy coding!