In this Python tutorial, we are going to discuss the top 8 Django commands which should be known to a beginner-level Django developer. So let’s get started with our discussion.
Also read: An introduction to Django Forms
Top 8 Django Commands for Beginners
Without any further ado, let’s get started with the different Django commands that you must know as a beginner to get your work done easily. While you can always go the traditional route and use a mouse and keyboard, and work with the GUI, that’s not a programmer’s way! And that’s why I decided to compile this quick list.
1. Create a Django project
If you are familiar with Django, you know that Django provides an initial collection of files, folders, and settings to start our project. To create the initial Django project structure, open your OS terminal and
cd to the directory or folder where you want to keep your Django project code. Then run the following command on the chosen terminal which will create a directory/folder with the <project_name> provided in the command within the present working directory.
> django-admin startproject <project_name>
2. Make migrations command
To convert the Python code written for the model classes (which further represents tables in the database) into database queries. And it becomes necessary to run this command whenever we make any kind of changes to our database class models. To run the following command move inside the project’s folder which contains the
manage.py file which will create the necessary database files inside the main project directory.
> python manage.py makemigrations
3. Migrate Command
We need to run this command to create tables in the specified database based on the defined Python class models. This command is responsible for applying or un-applying migrations. When we run this command for the first time all migrations related to the default apps (provided by the Django framework) are applied.
> python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK
4. Collect static files
In Django, we deal with the static files differently. It is advisable to create a separate folder and keep all the static files there. We need these Django commands to make it aware of the static files present inside the main project directory.
> python manage.py collectstatic
You have requested to collect static files at the destination
location as specified in your settings.
This will overwrite existing files!
Are you sure you want to do this?
Type 'yes' to continue, or 'no' to cancel:
5. Create a Django App
A Django project is a collection of apps and configurations for a website. A project can have multiple apps inside it and an app can be included in several Django projects. This command is required to create a Django app inside the Django project which will generate the basic directory structure of a Django app.
> python manage.py startapp <app_name>
6. Create a superuser
It is an essential and necessary command to log in to the default admin interface panel provided by the Django framework. This command is required to create a superuser for the Admin interface who has the username, password, and all other necessary permissions to access and manage the Django website.
> python manage.py createsuperuser
Username (leave blank to use 'inspiron'): Username
Email address: [email protected]
This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
7. Change the password
There are chances, we forget our password of the default admin interface panel. Then it becomes very much necessary to reset it because without a password we will not be able to get access to the default admin interface pannel. We must provide the appropriate <username> whose password has to be reset while running this command.
> python manage.py changepassword <username>
Changing password for user 'Username'
Password changed successfully for user 'Username'
8. Run server
It is again one of the very important and the most frequently used Django command. We need this command to verify and test our Django apps and websites by running them on the local server. By default, this command runs the Django development server on the internal IP at port number 8000. If we want, we can also change the development server’s IP and port number by sending them as command-line arguments.
> python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
August 30, 2021 - 15:16:23
Django version 3.2.5, using settings 'myproj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
In this Python tutorial, we have learned the top 8 Django commands which should be known to a beginner level Django developer. Hope you have understood the concepts discussed above and are ready to build your own Django Website or App. Thanks for reading! Stay tuned with us for more amazing learning resources on Python programming. Don’t forget to check out our Django tutorial series.