Have a look to the first two parts of this serie of
Django tutorials, to see how to install
virtualenv and django, and how to configure django with a mysql
database.
First a bit of vocabulary. We have seen that Django creates a package that contains the configuration of our website and various management scripts. The content of our website will be a single
web application that will process a user's input submitted through a form. Let's initialize this app:
python manage.py startapp textModif
This command will create a folder (a python package) named textModif.
geany textModif/*.py &
The most important part of our website's design comes now. We need to identify the pieces of data that we will manipulate in our web app. This is called the
model of our web app. We edit models.py and write:
from django.db import models
# Create your models here.
class TextTask(models.Model):
description = "A query submitted to the web app and the result of the web app operations"
originalText = models.CharField(max_length=10000)
finalText = models.CharField(max_length=10000)
task_date = models.DateTimeField('date requested')
def __unicode__(self):
return self.originalText
class TextOperation(models.Model):
description = "A type of operation the web app did on the submitted text"
modifType = models.CharField(max_length=1000)
modifDescription = models.CharField(max_length=1000)
def __unicode__(self):
return self.modifTypeclass TextModification(models.Model):
description = "A specific change the web add did on the text. For instance, delete a word in a sentence."
task = models.ForeignKey(TextTask)
operation = models.ForeignKey(TextOperation)
originalText = models.CharField(max_length=1000)
finalText = models.CharField(max_length=1000)
def __unicode__(self):
return self.originalText
Now we can indicate to Django that we want to use our web app in the scope of the django project on which we work (that I named "djangoProj_1"). To do this, we edit the settings.py of our project and add:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'textModif',
We request Django to create the tables in our database in accordance to our model:
python manage.py sql textModif
If everything looks ok; we can commit the
SQL configuration:
python manage.py syncdb
This commands configures mysql and also creates apis so that we can access the database conveniently through python instructions. Let's try a few things as in Django's official tutorial. We first launch django's python shell (it's configured with appropriate
environment variables)
python manage.py shell
Then, let's play
from textModif.models import *
from django.utils import timezone
# let's list our tasks (none exists for the moment)
TextTask.objects.all()
t1 = TextTask(originalText = "INFANDUM, REGINA, JUBES RENAVARE DOLOREM",
finalText = "Queen, you order to re-open cruel injuries",
task_date = timezone.now())
# let's list our tasks in the database (none exists for the moment as we have not committed our changes to mysql)
TextTask.objects.all()
t1.save
# let's see the id of our object in the database
t1.id
t1.originalText
Now our item is correctly saved in the database:
>>> TextTask.objects.all()
[]
Note that in the list, our object is described through a call to the "__unicode__" method that we have defined in models.py.