Thursday, January 05, 2006

Playing with Django-4

Now a project is created, so, the next thing to do, is to create the first application, as according to the tutorial, a polls application. To do so, just do this, startapp polls

A polls folder then created, as shown in the screenshot below.

The polls folder consists of structure like this,

This structure will house the polls application. Once this is in place, we have to define a model. A model is the single definitive, source of data about our data (based on what is said in the tutorial, :-) ).

Fo this simple polls app, we will create two models,

  • polls - polls and publication date
  • choice - text for the choice and vote tally
This concept will be implemented by modifying under polls/models,

the code look like this,

from django.core import meta

class Poll(meta.Model):
question = meta.CharField(maxlength=200)
pub_date = meta.DateTimeField('date published')

class Choice(meta.Model):
poll = meta.ForeignKey(Poll)
choice = meta.CharField(maxlength=200)
votes = meta.IntegerField()

With this, python could proceed to create a database schema and python database-access API.

So we have to first tell the system that polls app is installed by doing this: sql polls

Somehow an error appear, even after a few times of trying,

C:\Python24\Django-0.90\django\bin> sql polls
Traceback (most recent call last):
File "C:\Python24\Django-0.90\django\bin\", line 139, in ?
File "C:\Python24\Django-0.90\django\bin\", line 81, in main
File "c:\python24\lib\site-packages\Django-0.90-py2.4.egg\django\utils\transla", line 192, in activate
_active[currentThread()] = translation(language)
File "c:\python24\lib\site-packages\Django-0.90-py2.4.egg\django\utils\transla", line 111, in translation
from django.conf import settings
File "c:\python24\lib\site-packages\Django-0.90-py2.4.egg\django\conf\settings
.py", line 29, in ?
raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMEN
EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.

Finally, found out the error is with the Environment Variable not set, so before doing this step, just type

set DJANGO_SETTINGS_MODULE = myproject.settings


C:\Python24\Django-0.90\django\bin> sql polls
CREATE TABLE `polls_polls` (
`id` mediumint(9) unsigned auto_increment NOT NULL PRIMARY KEY,
`question` varchar(200) NOT NULL,
`pub_date` datetime NOT NULL
CREATE TABLE `polls_choices` (
`id` mediumint(9) unsigned auto_increment NOT NULL PRIMARY KEY,
`poll_id` integer NOT NULL REFERENCES `polls_polls` (`id`),
`choice` varchar(200) NOT NULL,
`votes` integer NOT NULL

And now as you can see it works. Sql is run and table is created.


timc3 said...

Thanks for the information on Django, it helped me get it installed on Windows XP. Perhaps you ought to add some information into the Wiki?

kahfei said...

Hi there,

wow, so my blog did contribute something to other also learning Django, I am really thrilled :-)

And get me really motivated too, Thanks, Tim.