From 0 to Senior in Python: Set up your first project with Django
Django is a super fast framework, based on Python. In this article I show you how to set up your very first project, with or without an virtual environment.
How to install Django
First and foremost: in order to work with Django you need to have Python installed on your computer.
Install Django globally
You can install Django globally or in your virtual environment. And of course, you can have it globally installed and also install it (maybe even in a different version) in your virtual environment.
The command to install Python in the same, regardless if you install it globally or in your virtual environment.
python3 -m pip install Django # Mac
py -m pip install Django # Windows
If you don’t know what a virtual environment is or how to set it up, I recommend to read my article How to set up a virtual environment for your Python project first.
Install Django in your virtual environment
I definitely would recommend always to use a virtual environment for your project. Here is how you do that:
- Create a project folder
- Set up the virtual environment
python3 -m venv .venv #Mac
py -m venv .venv #Windows
3. Activate the virtual environment
source .venv/bin/activate #Mac
source .venv/Scripts/activate #Windows
You can deactivate the virtual environment with this command:
deactivate # the same for Mac and Windows
4. Install Django
Navigate to the top of your project folder and run the command to install Django.
python3 -m pip install Django # Mac
py -m pip install Django # Windows
Congratulations. 👏 You just installed the Python Web framework Django in a virtual environment.
Let’s create a project now.
Create a project with Django
Make sure, that you are in the top level of your project folder. So Django will create the project one the same level as your .venv folder.
The following command creates a new project:
django-admin startproject yourProjectName
Run Django
Navigate into the root folder of your project.
Start the server with the following command:
python3 manage.py runserver # Mac
py manage.py runserver # Windows
The default port for Django is 8000. You can set another port by adding the number of the port after runserver
. If you don’t do that, Django starts on the default port.
#starts Django on port 8001
python3 manage.py runserver 8001 # Mac
py manage.py runserver 8001 # Windows
Well done. 👏 You good to got with your project now.
If you learned something from this article, leave me clap, a comment or share this article to support me. I welcome you to follow me here or on LinkedIn.