In this post, I will show you how to install Django and create your first project. We will be using Python's virtual environment. This is Django's recommended way to install Django and at the same time a best practice.
Python virtual environment installation
By installing Django inside a Python virtual environment, it allows us to run different Django projects without having them interfere with each other.
Make sure you already have Python and pip installed, if not refer to this post:
If you have not set up a Python virtual environment before, then refer to the post below. The virtual environment is recommended to go get the best out of Django.
1. Install Django
We will start by navigating to our Python virtual environment. Press the Windows Key on your keyboard, type
cmd, and press enter to open the terminal.
cd followed by the name of your path:
cd C:\Users\tijnv\Documents\LiveProjects\Codeonwards and press enter.
After that we have to start our virtual environment by use the following command:
project-name\Scripts\activate.bat, make sure you are using backslashes
Next navigate inside your Python virtual environment, by typing
cd project-name and run the Django installation command:
python -m pip install Django.
As you can see the installation ran, and we can check if the installation was successful by running the following command:
python -m django --version.
2. Create your first project
To create a new project we can run the following command:
django-admin startproject project_name.
Your new project has been created! You can check out your new project by navigating inside your project by typing:
cd project_name and then run the following command:
python manage.py runserver.
This will start your server, and in the console, you can see the browser link of your new project.
You can paste this link into your browser, and you will see that our installation was successful!
3. Run the migrations
Before we finish this installation, as you can see we got a message in our console that said:
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
These migrations are used to fill your database, run the following command to run the migrations:
python manage.py migrate.
4. Finishing touches
As you can see in the previous print screen our terminal was all in white text. I prefer to have some colors in my terminal to help me understand better what is going on. It turns out that we can easily do that by running the following command:
python -m pip install colorama.
As you can see at the bottom of the print screen we do have colors now.
In this post, we learned how to install Django and create our first project. By following the recommended practice of using Python's virtual environment, we ensured a clean and isolated setup for our Django projects.
We installed Django, created a new project, and started the server to see our project in action. We also addressed pending migrations and enhanced the terminal experience with colors. If you want to dive in further check out the post below to set up a MySQL connection with Django.