Learn Django

Install & Setup Django

In this lesson you'll setup all the base components required for the CRM Easy project. This includes creating a working directory, setting up a virtual environment, installing Django, creating a Django project, and configuring Git.

Step 1: Create a Working Directory

Like you did in the Hello, World project, you need to have a directory for this project. Find a good place on your computer and create the directory as follows (using a terminal or command prompt).

On a Mac

1
2
3
4
5
6
7
8
9
# take note of your directory
$ pwd
/<your_path>/

# create the crmeasy directory
$ mkdir crmeasy

# change directory
$ cd crmeasy

On Windows

1
2
3
4
5
6
7
8
9
# take note of your directory
C:\ pwd
C:\<your_path>

# create the crmeasy directory
C:\ mkdir crmeasy

# change directory
C:\ cd crmeasy

Step 2: Sync to Online Repo & Checkout

In the Computer Setup chapter you installed a version control system called Git. Git will be used in two different ways in this course. First, it will be used to deploy versions of the application to the hosting provider, Heroku.

Second, it will be used to help guide you through the course project. At key points in the course you'll be able to reset your project files to a known working version. This is made possible by completing this step, which will help you download a fully functioning version of the project.

Execute the commands below to download the project on your computer. These instructions will work on both Windows and Macs.

1
2
3
4
5
6
7
8
# clone the main repository; don't forget the . at the end
$ git clone https://github.com/tabdon/crmeasyapp .

# checkout version of project for chapter 3
$ git checkout step-0

# create a new branch 
$ git checkout -b setup_crm_easy_project

Step 3: Create a Virtual Environment

Next, we want to create and activate a virtual environment within our project directory. Execute these commands to do so.

In the virtualenv command below you will use the -p option to point to the proper Python 2.7 version. I do this because I'm not sure if you have more than one version of Python installed on your machine. The paths following the -p option are using the default locations for each OS and should work. However, if you installed Python to a different location the command will fail.

If you run the command and it doesn't work, please try to find where Python 2.7.x is installed on your machine and update the path following -p. If you need help please add a comment below.

On a Mac

The below path after -p is the default Python is installed if using MacPorts. If you installed Python by another means then it may be in a different folder.

1
2
3
4
5
# create the virtual environment
$ virtualenv venv --no-site-packages -p /opt/local/bin/python2.7

# Mac: activate the virtual environment
$ source venv/bin/activate

On Windows

Note that if you installed Python 2.7.x to a folder other than C:\Python27, then you'll need to change the path below to the proper install location.

1
2
3
4
5
# create the virtual environment, referring to the Python 2.7 executable
$ virtualenv venv --no-site-packages -p C:\Python27\python.exe

# Windows: activate the virtual environment
C:\...\crmeasy> venv\Scripts\activate

Step 4: Install Django

With the virtual environment created and activated, we can now install Django. In this step you'll use the Python package manager pip to install Django.

With the same terminal open and virtualenv activated, execute this command.

1
2
3
4
5
6
7
# install Django command
(venv)$ pip install Django==1.7.1
Downloading/unpacking Django==1.7.1
  Downloading Django-1.7.1-py2.py3-none-any.whl (7.4MB): 7.4MB downloaded
Installing collected packages: Django
Successfully installed Django
Cleaning up...

Step 5: Create the Django Project

Django has a concept of projects. A Django project is really just a collection of required files and directories. They are the high level organization unit of Django applications. Creating a project is simple and is completed by executing a single command.

At your terminal execute the following command.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# create the crmapp project NOTE: there's a period at the end of this command
(venv)$ django-admin startproject crmapp .

# On a Mac: list directory files 
(venv)$ ls -al
drwxr-xr-x  5 staff  staff  170 Nov 22 09:29 .
drwxr-xr-x  9 staff  staff  306 Nov 22 09:27 ..
drwxr-xr-x  6 staff  staff  204 Nov 22 09:29 crmapp
-rwxr-xr-x  1 staff  staff  249 Nov 22 09:29 manage.py
drwxr-xr-x  6 staff  staff  204 Nov 22 09:28 venv

# On Windows: list directory files
(venv) C:\crmeasy> dir
01/25/2015  02:39 PM    <DIR>          .
01/25/2015  02:39 PM    <DIR>          ..
01/25/2015  02:39 PM    <DIR>          crmapp
01/25/2015  02:39 PM               249 manage.py
01/25/2015  02:33 PM                33 README.md
01/25/2015  02:35 PM    <DIR>          venv
               2 File(s)            282 bytes
               4 Dir(s)  211,374,256,128 bytes free

Track your progress with a free account