In this exercise, we will be:

  1. Installing Python

  2. Setting up a Virtual Environment

  3. Installing Django

  4. Creating/Running a Django site

Installing Python and pip

Python is the programming language that Django uses. You can download the latest version here.

Windows

You can test to see if you have Python installed by running py --version in a terminal. You should see something > 3.8.

MacOS

MacOS comes with Python 2, which is depreciated. You can test to see if you have Python 3 installed by running python3 --version in a terminal. You should see something > 3.8.

Creating an Example Project Directory

Change to your home directory, create a directory named is601_example_project, and create a virtual environment within it:

PS C:\Users\rxt1077\IS601> cd ~
PS C:\Users\rxt1077> mkdir is601_example_project


    Directory: C:\Users\rxt1077


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         5/26/2021   3:33 PM                is601_example_project


PS C:\Users\rxt1077> cd is601_example_project
PS C:\Users\rxt1077\is601_example_project> py -m venv virtual_env
Everything after the prompt (> in my case) are the commands that you will type in. You can see the standard responses after each command.
Output and prompt may look slightly different on MacOS. Also Mac users may need to use the python3 command instead of the py launcher.

Activating a Virtual Environment

You now have a directory with a virtual environment in it. Whenever you open a new terminal to work on your project, you will need to activate this environment. Here is how that looks from PowerShell in Windows:

PS C:\Users\rxt1077\is601_example_project> virtual_env\Scripts\activate.ps1
(virtual_env) PS C:\Users\rxt1077\is601_example_project> (1)
1 Notice how the prompt has changed to show you are inside the virtual environment
On MacOS you can activate your environment with source virtual_env/bin/activate.
If you get the error "execution of scripts is disabled on this system" try running Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

Installing Django

pip is the package manager for Python. Django is distributed as a pip package, making it very easy to install in our new virtual environment:

(virtual_env) PS C:\Users\rxt1077\is601_example_project> pip install django
Collecting django
  Downloading Django-3.2.3-py3-none-any.whl (7.9 MB)
     |████████████████████████████████| 7.9 MB 6.4 MB/s
Collecting asgiref<4,>=3.3.2
  Downloading asgiref-3.3.4-py3-none-any.whl (22 kB)
Collecting sqlparse>=0.2.2
  Using cached sqlparse-0.4.1-py3-none-any.whl (42 kB)
Collecting pytz
  Downloading pytz-2021.1-py2.py3-none-any.whl (510 kB)
     |████████████████████████████████| 510 kB 6.8 MB/s
Installing collected packages: sqlparse, pytz, asgiref, django
Successfully installed asgiref-3.3.4 django-3.2.3 pytz-2021.1 sqlparse-0.4.1
WARNING: You are using pip version 21.1.1; however, version 21.1.2 is available.
You should consider upgrading via the 'c:\users\rxt1077\is601_example_project\virtual_env\scripts\python.exe -m pip install --upgrade pip' command.

Creating/Running a Django Project

The django-admin script is used to create a project. We will create one named is601_example_project inside the current directory:

(virtual_env) PS C:\Users\rxt1077\is601_example_project> django-admin startproject is601_example_project . (1)
1 No output means everything worked!

Now we will run the development server:

(virtual_env) PS C:\Users\rxt1077\is601_example_project> py manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, conten
ttypes, sessions.
Run 'python manage.py migrate' to apply them.
May 26, 2021 - 15:54:14
Django version 3.2.3, using settings 'is601_example_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Lastly visit http://localhost:8000 in a web browser and you should see a message stating, "The install worked successfully! Congratulations!" Take a screen shot and submit that as your work for this exercise.

To shut everything down type Ctrl+C (hold Control and press C at the same time) in your terminal window.