Django tutorials

We are going to create a django project that will be version controlled with git.

Create a git repository

Make sure that git is installed with:

$ sudo apt-get install git

Then you can initialise the git repository

$ mkdir myproject
$ cd myproject
$ git init

Create a virtual environment

To do that you can use virtualenv

$ sudo apt-get install virtualenv

virtualenv is a tool to create isolated Python environments.


Within myproject created above, you can create the virtual environment with:

$ virtualenv venv

The directory structure within myproject should look like:

└── venv
    ├── bin
    ├── include
    ├── lib
    └── local

NB: you can easily view the directory structure with the command tree (sudo apt-get install tree).

Then, we want to activate the virtual environment:

$ source venv/bin/activate

NB: to quit the virtual environment, use deactivate

Configure the virtual environment

This environment will be used only for this project, we are going to install the dependancies within this environment so that we can easily reinstall them with pip and a requirement.txt file.

If you do a pip freeze, you will see all the libraries installed.

If you install a new library, you will see that it has been added if you do a pip freeze again.

For instance, we want to install south, django-registration and stripe:

$ pip install django==1.5.1 south django-registration stripe
$ pip freeze
$ pip freeze

NB: you can precize the version you want. Here, we want to use django with the version 1.5.1

We are going to keep the output of git freeze in a requirements.txt file:

$ pip freeze > requirement.txt

That way, we can recreate the virtual environment easily with:

$ pip install -r requirement.txt

The virtual environment is going to take a lot of place in the git repository, so we are going to untrack it.

To do that, we are going to create a .gitignore file.

$ cat .gitignore

Create your django project

$ startproject myproject

Check that it is installed correctly by running the server:

$ python runserver

and in your browser, go to the url