Friday, July 10, 2009

Using django - database

Django seldom uses SQL. It uses python class to access DB.

Pros. You can apply one code to various types of DB.

In django database,
- Data type is equal to table.
- Class member is like a table field.

Designing data model

Create data type(table) in ur sampleapp/
from django.db import models

class SampleTable(models.Model):
  fieldA = models.IntegerField(unique=True)

Like IntegerField, there are also pre-defined field.
- TextField, DateTimefield, EmailField, URLField, FileField

Creating data type

Check if you activated your application in

Do DB sync
> ./ syncdb

You can see which SQL were generated with,
> ./ sql sampleapp

Note that it generate 'id' field automatically when it makes table.

Using DB Shell

Like we use database console to execute SQLs in terminal,
django also has shell.
> ./ shell
>>> from sampleapp.models import *
>>> record = SampleTable(fieldA=123)
>>> record.fieldA
>>> records = SampleTable.objects.all()
>>> firstRecord = SampleTable.objects.get(id=1)
>>> record.delete()

Like above, you can create a record, save it into DB, get all records or single one, access by id and delete it.

Note that until you save(), it just remain only in memory.

User data model

'User' data model is already built in.

>>> from django.contrib.auth.models import User
>>> User.objects.all()

User has many attributes like username, email and password.

Thursday, July 9, 2009

Using django - beginning


> sudo apt-get install python-django

Start project

> django-admin startproject sampleproject

Then, directory sampleproject is created.
You can run test server using in the directory.
You can set details using
You can set urls using

Setting DB(ex. SQLite)


Then create DB.
> ./ syncdb
- Chose if you will set superuser account.

Run server

> ./ runserver

Check it with url 'http://localhost:8000/'
(8000 is default.)

Create application

In one project, you can have several web-applications. Django calls it as applications.

Create with commands,
> ./ startapp sampleapp

Directory sampleapp is then generated.

Application is recommended to be designed with MVC model.

In sampleapp directory,
You can define http request handler function in the file

Implementing handler

from django.http import HttpResponse

def main(request):
  output = "<html>Welcome!</html>"
  return HttpResponse(output)

'request' has many informations about current requests.

from sampleapp.views import *
urlpatterns = patterns('',
  (r'^$', main)

In 'r'^$', r means regular expression, ^ is first of uri and $ stands for the last.
So it means '/' actually. Function main has been set as a handler for '/' above.

Now, check your first page with browser using 'http://localhost:8000/'. Good job!

Regular expressions

You can use expressions like below
. ^ $ * + ? | [a-z] \w \d
\w means alphabet or '_'
\d means one letter digit.

Wednesday, July 8, 2009

Disable firefox disk cache

Enter about:config in search bar.

set browser.cache.disk.enable as "false"

set browser.cache.disk.capacity as "0"

set browser.cache.memory.capacity (ex. 14336 (256M))