Installing and Configuring Django: for the begginer

Django attracts many non-programmers or programmers that do not have any experience with python. They all run into the same issues while installing and configuring Django. this article is an intent to put an end to all the issues while installing Django. This article is an attempt to put an end to all the issues with installing Django, and is for people running Ubuntu.

Before continuing any further, lets make sure you are running a modern* version of python.

Open a new terminal window and run the following command.

python -c 'import sys; print sys.version[:3]'

This command will output the python version you are running. I recommend using python 2.6 or 2.7. As a side note, Django doesn't work with python 3+. If you are running an older version of python, you will still be able to install Django but part of this tutorial may not apply to your situation.

Make sure you read the articles and follow their instructions before continuing.

Steps to install django on ubuntu.

  1. 1. make a fresh virtualenv by running the following command at the terminal:
    1. mkvirtualenv helloworld --distribute --no-site-packages
  2. 2. Activate the virtualenv by running:
    1. workon django
  3. 3. Install django(Note that we will be installing the newest version of django you could however install a different version if you so decide):
    1. pip install Django==1.4
  4. 4. Voila!!

Congratulations! You now have a running version of django! That wasn't hard at all, now was it?

Meet django projects and django apps.

One of the most debated issues with Django is the difference between an App and a Project. For now it will suffice to know that a project contains multiple apps, and a project more or less equals a website, and an app is just a functionality your website has. Let's go ahead and create our first project!

#make a new directory

mkdir www && cd www

#start a new django project

django-admin.py startproject helloworld

#cd into the newly create project

cd helloworld

This will create the following directory structure:

  1. /home/user
    1. /www
      1. /helloworld
        1. manage.py
        2. /helloworld
          1. __init__.py
          2. settings.py
          3. urls.py
          4. wsgi.py

Now use your favorite editor, I recommend using Vim or Gedit, and open the file settings.py. This is what the file should look like:

We need to make some changes to this file to have everything working on our local machine.

I always start my making imports on top of the file and setting my root directory variable.

import os #this setting allows us to use relative directory paths ROOT_PATH = os.path.dirname(__file__)

Then I add myself as one of the admins.

ADMINS = ( ('ubuntu Rocha', 'info@rochApps.com'), )

Assuming you have a database name helloworld and are running postgresql as your databse server.

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'helloworld', # Or path to database file if using sqlite3. 'USER': '', # Not used with sqlite3. 'PASSWORD': '', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } }

Point your media and static directory to the right paths.

MEDIA_ROOT = os.path.join(ROOT_PATH, '../media') MEDIA_URL = '/media/' STATIC_ROOT = os.path.join(ROOT_PATH, '../static')

Those two directories(media, static) do not exist yet. You need create them on the same level as your manage.py file. Let's go and create a templates directory that we are going to need next.

mkdir media static templates

Point your templates directory to the right path.

TEMPLATE_DIRS = ( os.path.join(ROOT_PATH, '../templates'), )

Uncomment the corresponding lines of code to have your INSTALLED_APPS variable look like this.

INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'django.contrib.admindocs', )

Your final version of the settings.py file should look something like this: