Creating Your First Django Project
In this blog post, I will show you how to create your own Django project (or web app) and have it run on your local machine. After you’re done with this tutorial, you’ll have a running website that displays the Django splash screen as shown here:
The steps that we’ll follow to build our Django project are the following:
- Create a virtual environment on Python 3.9
- Activate the virtual environment
- Install the Django framework in the virtual environment, and
- Create (and run) your first local Django project
1. Creating a virtual environment
Python virtual environments allow you to create separate environments for your Python projects; each environment typically contains a Python interpreter and the packages needed for your project.
I usually create a separate virtual environment for each new Python project that I start (regardless of weather it’s a Django project or not). If I need a new package for a specific project, I download it in the virtual environment associated with that project; if at some point I stop using a package in a particular project, I can simply remove it from the virtual environment, without having to worry about breaking other projects.
Let’s create a folder on the Desktop and go inside that folder:
cd ~/Desktop # The ~ is a shortcut for the 'home' folder
mkdir FirstDjango # mkdir will 'make' a new 'directory'
cd FirstDjango # cd will 'change directory' to something
Before we create a virtual environment, let’s check what python versions we have; we can do that by typing
python in the terminal, then double tabbing (pressing tab twice). Doing so on my computer gives me the following:
If you don’t have the Python version that you need, you can always go to https://www.python.org/ and download that version.
Let’s create a Python 3.9 virtual environment; we can do that by running the following command in the terminal:
python3.9 -m venv my_venv
-m stands for module, and it should be following by an existing module name
venv is a built-in module that helps us create virtual environments
my_venv is the name of the folder that will contain the virtual environment files and folders. I usually use the name
venv for that folder, but to avoid confusion, I decided to pick something different than the module name.
Notice here that since we used Python 3.9, the virtual environment that will be created will contain Python 3.9 interpreter. If you need to create a virtual environment with a different Python version, you should use that version to create the environment. You can think of each virtual environment as a clone for the Python version you use to create it.
2. Activate the virtual environment
Now that we have our virtual environment created, let us activate it. Inside the folder “my_venv”, we have another folder with the name “bin”, and inside “bin” we have a file with the name “activate”, as be shown below:
We can use the “activate” file to activate our virtual environment by running the following in our terminal:
Now, running the command
which python should show us the path to the Python in the virtual environment:
We can deactivate the current active virtual environment by simply running the command
deactivatein the terminal
Let’s not deactivate the virtual environment yet; if you already deactivated it, go ahead and activate it again using
activate, and let’s install Django in the active virtual environment.
3. Install Django in the virtual environment
Before we go ahead and install Django, let me show you that Django is not installed yet. We can verify that by trying to
import django in our Python shell:
To install Django, we simply do the following:
pip install django
We can verify that django has been installed successfully by trying to
import django in our Python shell.
Great, everything seems to be working okay. Now let’s do the last step.
4. Create (and run) your local Django project
Now that we’ve installed Django successfully, we should be able to use a new command-line called
django-admin to create Django projects. Keep in mind that you shouldn’t run this command in the Python shell, so let’s exit the shell by running
exit() if you’re still in the shell.
django-admin command that will create a Django project with the name “ProjectMedium00”:
django-admin startproject ProjectMedium00
Running the command above will create a folder with the name “ProjectMedium00” that contains few files and one other folder (with the same name). Here’s how the structure of the folders and files look like:
We won’t be talking about any of these files in this blog post. In other future posts, we’ll edit the “settings.py” and the “urls.py”. Let’s use “manage.py” to start our local server (i.e. start our website).
We can do that by going inside the first/upper “ProjectMedium00” folder, then running the following command:
python manage.py startserver
If we did everything right, we should see the following:
And there you have it, you just created a Django project. To exit and stop the server from running, we can simply Control+C in our terminal.
1. Should I create a virtual environment for each project?
Yes, it is generally a good practice to create a separate Python virtual environment for each project you’re working on, Django or otherwise. I (the author) always do that.
2. Do I always need to use the terminal to create a Django project?
No, you can actually use an IDE instead. PyCharm for example allows you to create a Django project without doing anything in the terminal. I prefer to create Django projects in the terminal however, then edit the files in PyCharm.
3. What is ‘pip’ exactly?
Pip is a command-line utility that helps us with installing, uninstalling, and updating Python packages. Starting a command with pip (such as
pip install django) will install the package(s) into the active virtual environment, so make sure to activate the virtual environment you’d like to install the package into before you pip install. You can also check the path of pip by running
which pip, which should point you to the directory of the active virtual environment.
4. What is ‘manage.py’ in our Django project?
It’s a command-line utility that lets you do things (such as starting your site, creating new apps in your site, and updating your database). We’ll use “manage.py” more in the future; for now, we only need it to run/start the server or our site.
5. I have my first Django project working now, what’s next?
What we should do next is create an app for our Django site and add some HTML files to it. We’ll do that in another blog post.