Before you can start learning Django, you must install some software on your computer. Fortunately, this is a simple three-step process:
- Install Python
- Install a Python Virtual Environment; and
- Install Django
This chapter is mostly written for those of you running Windows, as most new users are on Windows. I have also included a section on installing Python 3 and Django on macOS. If you are using Linux, there are many resources on the Internet—the best place to start is Django’s own installation instructions.
For Windows users, your computer can be running any recent version of Windows (7, 8.1 or 10). This chapter also assumes you’re installing Django on a desktop or laptop computer and will use the development server and SQLite to run all the code in this book. This is by far the easiest and best way to set up Django when you are first starting.
A lot of Windows applications use Python, so it may be already installed on your system. You can check this out by opening a command prompt, or running PowerShell, and typing
python at the prompt.
If Python isn’t installed you’ll get a message saying that Windows can’t find Python. If Python is installed, the python command will open the Python interactive interpreter:
Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) ... on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
You can see in the above example that my PC is running Python 3.9.7. Django 3.2 is compatible with Python version 3.6 and later. Django 4.0 is compatible with Python version 3.8 and later.
As Django 3.2 is the last series to support Python 3.6 and 3.7, the best version of Python to use with both Django 3.2 and Django 4 at this time is Python 3.9.
If you have an earlier version of Python on your machine, you must install a newer version of Python for the code in this book to work.
Assuming Python 3 is not installed on your system, you first need to get the installer. Go to https://www.python.org/downloads/ and click the big yellow button that says “Download Python 3.x.x”.
As I write this (early October 2021), Python just updated to 3.10.0. This release might have issues with the latest release of Django 3.2. To be safe, you can scroll down the page a bit and download the lastest Python 3.9.x release, as Python 3.9 works with both Django 3.2.x and Django 4.x.
Once you have downloaded the Python installer, go to your downloads folder and double click the file
python-3.x.x.msi to run the installer. The installation process is the same as any other Windows program, so if you have installed software before, there should be no problem here; however, there is one essential customization you must make.
By default, the Python executable is not added to the Windows PATH. For Django to work correctly, Python must be listed in the PATH statement. Fortunately, this is easy to rectify—when the Python installer screen opens, make sure Add Python 3.x to PATH is checked before installing (Figure 2-1).
Figure 2.1: Check the “Add Python 3.9 to PATH” box before installing.
Once Python is installed, restart Windows and then type
python at the command prompt. You should see something like this:
C:\Users\nigel> python Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) ... on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
Installing Python on macOS
If you open a terminal and type
python at the prompt, you will see that the system version is Python 2 (Figure 2-2). Django is not compatible with Python 2, so we need to install the latest version of Python 3.
Figure 2-2: macOS uses Python 2, which is incompatible with Django.
Downloading a copy of Python 3 follows the same process as Windows—Go to https://www.python.org/downloads/ and click the big yellow button that says “Download Python 3.x.x”. Your browser should automatically detect that you are using macOS and take you to the correct download page. If it doesn’t, select the correct operating system from the links below the button.
The Mac installer is in
.pkg format, so once it’s downloaded, double-click the file to run the package installer (Figure 2-3).
Figure 2-3: Follow the prompts to install Python 3 on macOS.
Follow the installations steps and, when Python 3 has been installed, open a new terminal window. If the installation was successful, typing
python3 at the prompt will open the Python 3 interactive shell (Figure 2-4). Note that macOS will happily run multiple versions of Python on the one machine, you just need to make sure you select the correct version when running the terminal.
Figure 2-4: Once Python 3 is installed, run it from the terminal with the
Creating a Python Virtual Environment
When you are writing new software programs, it’s possible (and common!) to modify dependencies and environment variables that your other software depends on. This can cause many problems, so should be avoided. A Python virtual environment solves this problem by wrapping all the dependencies and environment variables that your new software needs into a filesystem separate from the rest of the software on your computer.
The virtual environment tool in Python is called
venv, but before we set up
venv, we need to create our club site project folder.
Create a Project Folder
Our project folder will house not only our virtual environment, but all the code and media for our Django club site.
The project folder can go anywhere on your computer, although it’s highly recommended you create it somewhere in your user directory, so you don’t get permission issues later on. A good place for your project in Windows is your My Documents folder. On a Mac your Documents folder is also a logical choice; however, it can go anywhere in your user directory.
Create a new folder on your system. I have named the folder
myclub_project, but you can give the folder any name that makes sense to you.
For the next step, you need to be in a command window (terminal on Linux and macOS). The easiest way to do this in Windows is to open Windows Explorer, hold the SHIFT key and right-click the folder to get the context menu and click on Open command window here (Figure 2-5).
Figure 2.5: Hold the shift key and right-click a folder to open a command window.
Create a Python Virtual Environment
Once you have created your project folder, you need to create a virtual environment for your project by typing the following at the command prompt you just opened:
...\Documents\myclub_project> python -m venv env_myclub
...$ python3 -m venv env_myclub
Remember, you must be inside the project folder!
The function of this command is straightforward—the
-m option tells Python to run the
venv module as a script.
venv in turn requires one parameter: the name of the virtual environment to be created. So this command is saying “create a new Python virtual environment and call it env_myclub”
venv has finished setting up your new virtual environment, it will switch to an empty command prompt. When it’s done, open Windows Explorer and have a look at what
venv created for you. In your project folder, you will now see a folder called
\env_myclub (or whatever name you gave the virtual environment). If you open the folder on Windows, you will see the following:
\Include \Lib \Scripts pyvenv.cfg
On a Mac, it’s:
/bin /Include /Lib pyvenv.cfg
On either platform, if you look inside the
\Lib folder, you will see
venv has created a complete Python installation for you, separate from your other software, so you can work on your project without affecting other software on your system.
To use this new Python virtual environment, we have to activate it, so let’s go back to the command prompt and type the following:
This will run the activate script inside your virtual environment’s
\scripts folder. You will notice your command prompt has now changed:
On a Mac, the prompt looks like this:
(env_myclub) ... <yourusername>$
Or, on newer versions of macOS:
(env_mfdw) user@computer mfdw_project %
(env_myclub) at the beginning of the command prompt lets you know that you are running in the virtual environment.
If you want to exit the virtual environment, you just type
deactivate at the command prompt:
(env_myclub) ...\Documents\myclub_project> deactivate C:\...\Documents\myclub_project>
Like the Content? Grab the Book for as Little as $14!
Other than providing you with a convenient resource you can print out, or load up on your device, buying the book also helps support this site.
eBook bundle includes PDF, ePub and source and you only pay what you can afford.
You can get the paperback from Amazon.
Now we have Python installed and are running a virtual environment, installing Django is super easy, just type the command:
pip install "django>=3.2,<4"
If you are not familiar with the
pip command, put briefly, it’s the Python package manager and is used to install Python packages. To keep with Python programming tradition,
pip is a recursive acronym for “Pip Installs Packages”.
This will instruct
pip to install the latest version of Django 3 into your virtual environment. Your command output should look like this:
Collecting django<4.0,>=3.2 Downloading Django-3.2.8-py3-none-any.whl (7.9 MB) |################################| 7.9 MB 364 kB/s Collecting asgiref<4,>=3.3.2 Downloading asgiref-3.4.1-py3-none-any.whl (25 kB) Collecting pytz Downloading pytz-2021.3-py2.py3-none-any.whl (503 kB) |################################| 503 kB 939 kB/s Collecting sqlparse>=0.2.2 Downloading sqlparse-0.4.2-py3-none-any.whl (42 kB) |################################| 42 kB 303 kB/s Installing collected packages: sqlparse, pytz, asgiref, django Successfully installed asgiref-3.4.1 django-3.2.8 pytz-2021.3 sqlparse-0.4.2
To test the installation, go to your virtual environment command prompt, start the Python interactive interpreter by typing
python and hitting Enter. If the installation was successful, you should be able to import the module
(env_myclub) ...\myclub_project>python Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38)... on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import django >>> django.get_version() '3.2.8' >>> exit()
Don’t forget to exit the Python interpreter when you are done (you can also use CTRL-Z).
You can also check if Django has been installed directly from the command prompt with:
(env_myclub) ...\myclub_project>python -m django --version 3.2.8 # Your version may be different.
Starting a Project
Once you’ve installed Python and Django, you can take the first step in developing a Django application by creating a Django project.
A Django project is a collection of settings and files for a single Django website. To create a new Django project, we’ll be using a special command to auto-generate the folders, files and code that make up a Django project. This includes a collection of settings for an instance of Django, database configuration, Django-specific options and application-specific settings.
I am assuming you are still running the virtual environment from the previous installation step. If not, start it again with
From your virtual environment command line, run the following command:
(env_myclub) ...\myclub_project>django-admin startproject myclub_site
This command will automatically create a
myclub_site folder in your project folder, and all the necessary files for a basic, but fully functioning Django website. Feel free to explore what
startproject created now if you wish, however, we will go into greater detail on the structure of a Django project in the next chapter.
Creating a Database
Django includes several applications by default (e.g., the admin program and user management and authentication). Some of these applications make use of at least one database table, so we need to create tables in the project database before we can use them. To do this, change into the
myclub_site folder created in the last step (type
cd myclub_site at the command prompt) and run the following command:
python manage.py migrate
The migrate command creates a new SQLite database and any necessary database tables, according to the settings file created by the
startproject command (more on the settings file later). If all goes to plan, you’ll see a message for each migration it applies:
(env_myclub) ...\myclub_site>python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK # several more migrations (not shown)
The Development Server
Let’s verify your Django project works. Make sure you are in the outer
myclub_site directory and run the following command:
python manage.py runserver
This will start the Django development server—a lightweight Web server written in Python. The development server was created so you can develop things rapidly, without having to deal with configuring a production server until you’re ready for deployment.
When the server starts, Django will output a few messages before telling you that the development server is up and running at
If you were wondering, 127.0.0.1 is the IP address for localhost, or your local computer. The 8000 on the end is telling you that Django is listening at port 8000 on your local host.
You can change the port by passing a port number to the runserver command, For example:
(env_myclub) ...\myclub_site> python manage.py runserver 7000
This command will start the development server at port 7000. It’s unlikely you will need do this, unless you have another program running at port 8000, or your computer administrator is blocking the port.
Now that the server is running, visit
http://127.0.0.1:8000/ with your web browser. You’ll see Django’s default welcome page, complete with a cool animated rocket (Figure 2-6).
Figure 2.6: Django’s welcome page.
In this chapter, I showed you how to install Python 3 and Django on both Windows and macOS. In the next chapter, we will step back a bit and have a big-picture look at Django’s structure and how all the parts of Django work together to create powerful, scalable web applications.