How to install and create your first Django project

Django Sep 12, 2021

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:

How to install Python and pip on Windows
It has been some time since I last used Python. So I wanted to make sure I have the newest version, but instead of upgrading, I decided to go with a fresh install. So in this post, I will show you how to install Python and pip and explain all the options.

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.

How to quickly set up a Python virtual environment
Setting up a Python virtual environment is very beneficial and I recommend everyone to do it. In this post, I will show you how to quickly set up a virtual environment for Python and explain to you why it is so valuable.

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.

First type 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 \.

install_django_1
Next navigate inside your Python virtual environment, by typing cd project-name and run the Django installation command: python -m pip install Django.

install_django_2
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.

install_django_3

2. Create your first project

To create a new project we can run the following command: django-admin startproject project_name.

create_django_project_1-1
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.

create_django_project_2-1

You can paste this link into your browser, and you will see that our installation was successful!

successful_django_installation

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.

create_django_project_3

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.

create_django_project_4

As you can see at the bottom of the print screen we do have colors now.

5. Conclusion

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.

How to set up a MySQL database connection in Django
In this post, I will show you how to set up a MySQL database in Django. MySQL is not the only database that is supported by Django. But in this post, we will only connect to a MySQL database. Requirements * Django project * MySQL database Connection * MySQL database Click the links

Tags