Personal tools
You are here: Home Members jan's Home Building Computerbank's database with Django
Document Actions

Building Computerbank's database with Django

D R A F T - A emerging set up guide for the Computerbank Django web based database using industry standard open source software.

These instructions are for people joining the Computerbank Database group and for people interested in a real world Django project. 

Email jan@computerbank.org.au if you are interested in joining this voluntary team. Python knowledge is an advantage.

Our code our discussions


Why Django

We chose the Python based Web Application Framework Django because Django includes the tools necessary to add a web front end to a relational database and removes the need to use SQL.  We also favoured Django because it uses Python, is component architecture, is open source software and has good  documentation.  The database we are using is PostgreSQL.


Recommended Reading

  • Apache Web Server set up (to come)
  • Beginning Databases with Postgresql, 2nd Edition, Apress.
  • Cascading Style Sheet links  Django uses css for layout and text attributes.
  • Django Tutorial  This excellent online tutorial gives the basics on how to connect the Django front end to a database and then goes on to build a Poll Product.  From this foundation it is relatively easy to add your own customisations.
  • Django's New forms How to create html forms in django using the 'newforms' library. These forms sit between the template files and the datbase models - so they are somwhere between the actual "output html" which will end up representing the form, and the back-end database tables which is where the data from the form will end up going (these tables correspond to  "models" in django's terminology).
  • Django Templating Language The template language contains a mix of ordinary html and django "tags", and python "variables. The python variables will contain text to replace parts of the template, and the "tags" can perform some elementary logic such as if: then and looping over a list of items. There are some very useful inbuilt tags for fromatting text and so on.
  • Learning jQuery - a powerful JavaScript library and cross browser framework which we have integrated into our Django.
  • Python  - Django is developed in the Python programing language.
  • Regular expressions how-to Django uses regular expressions to ensure the integrity of data entered into the database
  • Ubuntu Linux


Database Design

Before we began coding the database we examined the existing Computerbank database. We then asked the users of the Database their experiences and suggestions. We compiled a set of actual and requested use cases.
We also looked at a similar database in use at Free Geek in the US and chatted to one of their key developers via Internet Relay Chat.  From these foundations we made a draft of the database tables for phase one of the project.


Software needed for the development box running Debian stable or Ubuntu

apt-get install (on Debian or Ubuntu) the following software:


How to install Django 

These excellent notes are from the Django website.  Tip

Subversion - setting up your svn code repository

See Wendy Langer's svn setup steps.

This project's Subversion repository is hosted at Google Code.


PostgreSQL - setting up a user and database

su postgres -c psql template1
ALTER USER POSTGRES with password whateveryouchoose
\q
Now we go to the postgres pg_hba.conf file  which will be somewhere like /etc/postgres/8.2/main.  Open the file  with the editor nano
nano pg_pg_hba.conf
Go to the end of the file and comment out the lines that start with host.  The local line should be changed to look like:
local      all     all      password
(Note this set up is for a development machine only it is not recommended for a live site)

Restart Postgres
/etc/init.d/postgresql-8.2 restart
This is all you have to do to set up your relational database.  No sql is needed the rest of the database and web configuration is done via Python scripts in Django.




Django - Getting started


Django is the web front end framework for our PostgreSQLl relational database.  The next step if you are new to Django is to complete the free four part Django Tutorial available from the Django Project website.  This tutorial cover most of the basics of the Django component framework.  However, security, permissions, updating data models and external layout are not covered in the tutorials.

The Django Tutorials  show:
  •  How to connect the Django front end to a database.
  •  How to define a database schema in a model.
  •  How to activate a database table and a directory structure from a model.
  •  How to build a small web based application. 
  •  How to enable and customise the built-in administration area

Django - Customising the look of a Django web site

After completing the Django four part tutorial you will see the web pages of the Poll product lack layout and style.  Do not be tempted to add frames and tables for layout. Frames are obsolete and tables will slow down your website particularly if you put tables within tables. Use Cascading Style Sheets for layout and text styling.


Django - Adding External CSS and other Static Media to Django  by Wendy Langer

To get the Django webserver to correctly "serve" (make available for use) static files such as css, images, and external javascript, you need to make changes in three places:
  1. Make a folder to hold these 'static' files
  2. Tell the Django webserver to "serve" these files (settings.py)
  3. Tell Django how to map attempted url accesses of these files to real files on the file-system (urls.py)

Below is a more detailed step by step guide:

1. Make a directory called "sitemedia" under your application directory. In my case the application is called "contacts" and the project is called "wjContact", so I made a directory called [rest-of-path]/wjContact/contacts/sitemedia.

Underneath this I put three directories called css, js (for javascript), and img (for images). The stylesheets you want to use go into the css directory (obviously!)

This directory structure is copied from the Django "Admin" module which comes with every Django installation. We don't have to use this particularly directory structure, but I figured it was as good a place to start as any. This kind of structure is also used by another project I found that is being hosted by googlecode.(They probably copied it as well!)

Anyway, so what you end up with is:

  • [rest-of-path]/wjContact/contacts/sitemedia/css
  • [rest-of-path]/wjContact/contacts/sitemedia/js
  • [rest-of-path]/wjContact/contacts/sitemedia/media

and in the css directory is the stylesheet you want to use. Leave the other two directories empty for now.

2. Now you need to put the following into your urls.py:

 (r'^sitemedia/(?P<path>.*)$', 'django.views.static.serve', {
       'document_root': settings.MEDIA_ROOT[:-1]
   }),

3. And finally you need to put these changes into your settings file: (of course change the MEDIA_ROOT directory to suit your own system!)

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = "/home/wendy_admin/src/eclipse_workspace/djangodb/misc/wendy/wjContact/contacts/sitemedia/'

# URL that handles the media served from MEDIA_ROOT.
# Example: "http://media.lawrence.com"
MEDIA_URL = '/sitemedia/'

Django - Further references for adding static media to your Django site:


Django - Attaching CSS to your external Django pages

After adding media files such as css files to your Django application, the next step is to set up external html pages with links to CSS.

..........................to be continued..............................



Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: