Django Getting Started
To install Django, you must have Python installed, and a package manager like PIP.
PIP is included in Python from version 3.4.
Django Requires Python
To check if your system has Python installed, run this command in the command prompt:
python --version
If Python is installed, you will get a result with the version number, like this
Python 3.9.2
If you find that you do not have Python installed on your computer, then you can download it for free from the following website: https://www.python.org/
PIP
To install Django, you must use a package manager like PIP, which is included in Python from version 3.4.
To check if your system has PIP installed, run this command in the command prompt:
pip --version
If PIP is installed, you will get a result with the version number.
For me, on a windows machine, the result looks like this:
pip 20.2.3 from c:\python39\lib\site-packages\pip (python 3.9)
If you do not have PIP installed, you can download and install it from this page: https://pypi.org/project/pip/
Virtual Environment
It is suggested to have a dedicated virtual environment for each Django project, and one way to manage a virtual environment is venv, which is included in Python.
With venv, you can create a virtual environment by typing this in the command prompt, remember to navigate to where you want to create your project:
Windows:
py -m venv myproject
Unix/MacOS:
python -m venv myproject
This will set up a virtual environment, and create a folder named "myproject" with subfolders and files, like this:
myproject
Include
Lib
Scripts
pyvenv.cfg
Then you have to activate the environment, by typing this command:
Windows:
myproject\Scripts\activate.bat
Unix/MacOS:
source myproject/bin/activate
Once the environment is activated, you will see this result in the command prompt:
Windows:
(myproject) C:\Users\Your Name>
Unix/MacOS:
(myproject) ... $
Note: You must activate the virtual environment every time you open the command prompt to work on your project.
Install Django
Finally, we can install Django.
Remember to install Django while you are in the virtual environment!
Django is installed using pip, with this command:
Windows:
(myproject) C:\Users\Your Name>py -m pip install Django
Unix/MacOS:
(myproject) ... $ python -m pip install Django
Which will give a result that looks like this (at least on my Windows machine):
Collecting Django
Downloading
Django-4.0.3-py3-none-any.whl (8.0 MB)
|████████████████████████████████|
8.0 MB 2.2 MB/s
Collecting sqlparse>=0.2.2
Using cached
sqlparse-0.4.2-py3-none-any.whl (42 kB)
Collecting asgiref<4,>=3.4.1
Downloading asgiref-3.5.0-py3-none-any.whl (22 kB)
Collecting tzdata;
sys_platform == "win32"
Downloading tzdata-2021.5-py2.py3-none-any.whl (339
kB)
|████████████████████████████████| 339 kB 6.4 MB/s
Installing
collected packages: sqlparse, asgiref, tzdata, Django
Successfully
installed Django-4.0.3 asgiref-3.5.0 sqlparse-0.4.2 tzdata-2021.5
WARNING: You are using pip version 20.2.3; however, version 22.0.4 is available.
You should consider upgrading via the 'C:\Users\Your Name\myproject\Scripts\python.exe -m pip install --upgrade pip' command.
That's it" Now you have installed Django in your new project, running in a virtual environment!
Windows, Mac, or Unix?
You can run this project on either one. There are some small differences, like when writing commands in the command
prompt, Windows uses py
as the first word in the command line,
while Unix and MacOS use
python
:
Windows:
py --version
Unix/MacOS:
python --version
In the rest of this tutorial, we will be using the Windows command.
Check Django Version
You can check if Django is installed by asking for its version number like this:
(myproject) C:\Users\Your Name>django-admin --version
If Django is installed, you will get a result with the version number:
4.0.3
What's Next?
Now you are ready to create a Django project in a virtual environment on your computer.
In the next chapters of this tutorial we will create a Django project and look at the various features of Django and hopefully make you a Django developer.