Setting up Django

Starting Point

We’ll start off with some basics. Firstly, let’s get Django set up.

For the moment, I’ll be using a Fedora 10 Virtual Machine to get the basics up and running, we’ll transit to a live solution later on in the process - once we’ve got some of the basics up and running.

# sudo yum install Django
Loaded plugins: refresh-packagekit
dries                                                    |  951 B     00:00
fedora                                                   | 2.8 kB     00:00
updates                                                  | 3.4 kB     00:00
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package Django.noarch 0:1.1.1-2.fc10 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch            Version                 Repository        Size
================================================================================
Installing:
 Django          noarch          1.1.1-2.fc10            updates          3.9 M

Transaction Summary
================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 3.9 M
Is this ok [y/N]: y
Downloading Packages:
Django-1.1.1-2.fc10.noarch.rpm                           | 3.9 MB     00:04
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : Django-1.1.1-2.fc10.noarch                               1/1

Installed:
  Django.noarch 0:1.1.1-2.fc10

Complete!

Once that’s done, we’ll create a simple Django project:

# cd ~/my_codeplane
# django-admin startsite spengler_ng
# cd spengler_ng
# git init
# git add *
# git commit -a -m 'Initial code commit'

To make our lives a little easier later, we’ll tell Django we want to hold all of our template pages somewhere else - saves getting the main directory too cluttered! To do this, add the following to the settings.py:

    import os.path

    TEMPLATE_DIRS = (
        os.path.join(os.path.dirname(__file__), 'templates'),
    )

Lastly, tell Django we’re going to use a sqlite database (for internal testing anyway.) Edit settings.py to contain:

    DATABASE_ENGINE = 'sqlite3'
    DATABASE_NAME = 'spengler.sql'

To test what we have so far, run:

# ./manage.py runserver 0.0.0.0:3000

Then surf to http://192.168.230.219:3000 where you should see a nice “Welcome to Django” page.

For right now, I’m not going to add any particular content, and I’ll just skip ahead and install the task and queuing system(s). For this I’m going to use Celery and RabbitMQ.

Installing

# sudo yum install rabbitmq-server
# sudo yum install python-setuptools python-setuptools-devel
# sudo yum install pycurl
# sudo easy_install Celery
# sudo easy_install django-celery

Configure RabbitMQ

# sudo chkconfig rabbitmq-server on
# sudo service rabbitmq-server start
# sudo rabbitmqctl add_user username 123
# sudo rabbitmqctl add_vhost
# sudo rabbitmqctl set_permissions -p vhost username ".*" ".*" ".*"

Configure Django-Celery

Add the following to settings.py:

import djcelery
djcelery.setup_loader() 

And to installed apps:

'djcelery',

and also

BROKER_HOST = "localhost"
BROKER_PORT = 5672 
BROKER_USER = "username" 
BROKER_PASSWORD = "123"
BROKER_VHOST = "vhost" vhost

Now, we need to generate the celery databases (just this first time.)

# ./manage.py syncdb

So that we can start Celery as a service, grab the script from here - although the project contains a slightly modified version of it. This has been added a celeryd.sh to the git project. We also need the celerybeat script as well.

# groupadd celery
# useradd -g celery -M celery

# sudo chkconfig celeryd on
# sudo chkconfig celerybeat on
# sudo service celeryd start
# sudo service celerybeat start

Tags: django, python, celery, rabbitmq
blog comments powered by Disqus