If you're new to Django, then you might feel overwhelmed by the number of new Django commands to learn and find yourself needing to use the weird sounding commands time and time again. But what do they actually mean? In this article, I will explore the top most common, useful and must-know Django commands for all beginners.
1. Creating a new Django Project
To create a new Django project, we can run the following command on the root directory of where your project will be. You can replace "main" with a custom name for your Django project.
django-admin startproject main
This command creates a directory called "main". Inside it, there is a file called manage.py and another directory called "main". Why Django name the two directories the same is quite perplexing, so to avoid potential confusion in the future, I recommend renaming the parent "main" folder to "main-project" or something else that will meaningfully distinguish the two directories to you.
2. Run the server to see your Django Project
After running the command below, you'll be able to view your Django project on localhost:8000.
python manage.py server
If for any reason you want to run it on a different port, say 3000. Then you can specify the port number at the end of the command like so:
python manage.py runserver 3000
3. Make Migrations
Whenever there is a change to any of your existing Django models, you should run the following command to "update" them.
python manage.py makemigrations
After the process finishes, you will be able to see a new file created that updates your migrations. It essentially acts like a version control for your Django models as you'll be able to see how your models change over time.
4. Migrate
To make sure that your database runs with the latest version of your Django models, it is important that you run the following command. Normally, you would run makemigrations before running migrate. At the core, the migrate command essentially syncs your Django project with the settings specified in settings.py
python manage.py migrate
5. Collect Static Files
The following command instructs Django to search for all static files in your project (js, css, images) and collects them to a single directory that is specified in the STATIC_URL setting. It is important to note that you should never modify this single static directory manually (because things will stop functioning like it should) and that it should only be modified from the following command.
python manage.py collectstatic
6. Add a New Functionality
To add a new functionality to your Django project, running the following command will help you get started with the files you need. After, you'll need to add this "app" into INSTALLED_APPS list in settings.py
- Note: you can see an "app" as a "component" rather than actual apps
python manage.py startapp <app-name>
7. Create Admin User
To create a user that is able to access the admin page, we can run the following command. You will be prompted to enter a username, email address, and a password. The email address is optional and you can leave that blank if you'd like.
python manage.py createsuperuser
After you see the message "Superuser created successfully", you should be able to see that you can have access to the admin page by going to localhost:8000/admin after running the server, and log in using the newly created credentials.
Hopefully this article was able to help you understand common Django commands, understand what it is, and how or when to use it properly.
Good luck and thanks for reading!