Starting a Django project and deploying it to an Apache server - Part 1

Published byGoogle on

There are several ways to start a Django project, here we will be explaining how we do it. Our way is not supposed to be the "best way" nor the "only way", it's just our way and we would like to share.

To start our project we will use:

Before we get started you probably want to make sure you have Python, VirtualEnv, Mercurial and SQLite3 installed on your computer. SQLite3 is one of the database backends used by Django, it is suited for small databases often used during the develpment process. If you later need a more robust database engine, you can pick one of many available on Django.

Let's start things by initializing a Mercurial repository. Mercurial is a revision control tool. If you don't know what a revision control tool is, read the linked Wikipedia article it'll give you a good idea.
There are many revision control tools out there, we use Mercurial simply because we like it better than the others.
Once installed, all you need to do to initialize a mercurial repository. In this exemple our project will be called mybaseproject.
Type the following on your command prompt:

$ hg init mybaseproject

You can also clone a fresh remote repository.

 Now we'll start our VirtualEnv. VirtualEnv allows you to create a separate set of libraries aside from your "regular system". As several projects might need different versions of the same library or libraries incompatible with the ones of another project, it is always a good idea to have separate VirtualEnv's for each projects.
To start this project virtualenv, simply type:

$ virtualenv mybaseproject/

And we'll activate it:

$ source mybaseproject/bin/activate

This will prefix your prompt with (mybaseproject), meaning the wirtualenv is active and that it's libraries are available for use.
Every time you will want to work on your project in the future you will have to activate it's VirtualEnv first.

We will not need the VirtualEnv files when deploying on the production servers (websites do not run inside VirtualEnv's, well they can but we don't like the idea) so we have to tell Mercurial to ignore those files. Mercurial knows which files not to care about thanks to a file called .hgignore .
Edit this file with your favourite editor (we like to use Vim), so in our case we type:

$ vim mybaseproject/.hgignore

Here is what our .hgignore file looks like:

## we use vim so we don't want to version swap files
## when pulling conflicting revisions, mercurial creates a .orig file; We don't want those either
## compiled python source has a .pyc extension; We don't want those either

Now before we actually start our project we need to install Django.
When starting a new Virtualenv, one of things installed inside it is pip. Pip is the tool we are going to be using to install any needed libraries. One of the nice things about pip is that it manages dependencies so we can ask it to install any library we need and it will also install dependencies for us.
To install django with pip, simply type the following instruction at the command prompt:

$ pip install django

Now that django is installed, we can actually start our project. Of course we want our project inside the versioned directory, but django refuses to start a new project inside an existing directory. The way we do it to circumvent that, is that we start our project in a tempory directory and then move it inside our project's directory
At the command prompt, do the following:

$ startproject mybaseproject_tmp
$ mv mybaseproject_tmp/* mybaseproject/
$ mv mybaseproject/mybaseproject_tmp mybaseproject/mybaseproject
$ rm -rf mybaseproject_tmp

There's only a slight problem working like this, it's that django initated the new project naming it mybaseproject_tmp. So we have to change that in 4 different files

  • mybaseproject/
  • mybaseproject/mybaseproject/
  • mybaseproject/mybaseproject/
  • mybaseproject/mybaseproject/

Edit those files and replace any occurence of mybaseproject_tmp with mybasebaseproject.
If you want, on a unix shell, you can easily do this using sed by typing:

$ grep -rli mybaseproject  mybaseproject/mybaseproject/* mybaseproject/ | xargs \
> sed -ri 's/mybaseproject_tmp/mybaseproject/g'

Sed uses regular expressions. The one we gave it means Switch mybaseproject_tmp by mybaseproject while being Greedy. If you didn't understand everything, don't worry, we will probably have a "usefull regular expressions" tutorial in a near future.

It's time to move into our project directory by typing:

$ cd mybaseproject

One of the nice things about pip is that is allows you to list the libraries you installed with it and save that list to a file. That way you can easily replicate your environment on another machine (e.g. our production server).
Let's check it out:

$ pip freeze > requirements.txt

If you edit the file requirements.txt, you'll see a list of all the libraries installed by pip (probably just Django for now).

It is time to check out what files have been created and need to be commited to our repository. To do so we can use a simple command line:

$ hg status

This will output a list of files prefixed by ?, A, R, M. Here is the meaning of those prefixes:

  • ? - unversionned file
  • A - file which will be added on the next commit
  • R - file which will be removed on the next commit
  • M - file which has been modified since the last commit and whose modifications will be sent on the next commit

Here is what the command should output for you:

? .hgignore 
? mybaseproject/ 
? mybaseproject/ 
? mybaseproject/ 
? mybaseproject/ 
? requirements.txt

We will tell mercurial to add all those files to the repository and do our first commit by typing:

$ hg add .hgignore requirements.txt mybaseproject
$ hg ci -m "Our first commit"

We have commited our work, but we have never checked it was working... Let's do that now!

$ python runserver

And open and you should see something like the picture on the right

This is where we'll stop for today. Be sure to check out the second part of this tutorial next week.
In the mean time: Have fun Laughing

VirtualEnv South Tutorial Wsgi Apache Mercurial Django


Post a comment