8 Jan 2009

Django Project creation check list

1) (Optional) Create new Project: django-admin.py startproject projectname
2a) Add to Apache config (replace evreything within <>):

<Location "/<location>/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE ikariam.settings

PythonDebug On # disable this on a production site!

PythonPath "['<path to project root>'] + sys.path"
</Location>
# media files
Alias /media "/usr/lib/python2.4/site-packages/django/contrib/admin/media"

2b) Optionally run the python webserver: python manage.py runserver 8080
3) settings.py: Set TIME_ZONE = 'Europe/Zurich' and LANGUAGE_CODE = 'de-CH'
4) settings.py: Set Database config
5) settings.py: Add project
INSTALLED_APPS = (
'mysite.books', # or
'mysite'
)


6) example Model:
# $Id$
#
# if not specified, primarykeys will be generated automatically. A field
# with the name `id` is added as auto increment field. So do not specifically
# declare primarykeys manually unless needed.
# http://docs.djangoproject.com/en/dev/topics/db/models/#automatic-primary-key-fields
from django.db import models

class Schedule(models.Model):
# id = models.AutoField(primary_key=True)
start = models.TimeField()
end = models.TimeField()
lastmod = models.DateTimeField(auto_now=True)
inserted = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return self.start + " - " + self.end

class Price(models.Model):
# id = models.AutoField(primary_key=True)
name = models.CharField(max_length=10)
description = models.CharField(max_length=250)
lastmod = models.DateTimeField(auto_now=True)
inserted = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return self.name

class Event(models.Model):
# id = models.AutoField(primary_key=True)
name = models.CharField(max_length=30)
start = models.DateField()
end = models.DateField()

schedule = models.ForeignKey(Schedule)
price = models.ForeignKey(Price)

lastmod = models.DateTimeField(auto_now=True)
inserted = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return self.name

class Meta:
# db_table = u'ana_event'
ordering = ["name"]
verbose_name = "Event"
verbose_name_plural = "Events"

class Admin:
pass
# list_display = ("name", "start", "end")
# search_fields = ('name',)
# list_filter = ('name')
# ordering = ('x', 'y')

7) Check db creation and create Database:
python manage.py validate # validate db structure
python manage.py sqlall projectname # show creation SQL
python manage.py syncdb # create Database

8) Install the admin application. Do this by adding "django.contrib.admin" to your INSTALLED_APPS setting.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'ana'
)

9) Install url redirector
from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Example:
# (r'^ana/', include('ana.foo.urls')),

# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
# (r'admin/$', include('django.contrib.admin.urls')),
(r'^ana/admin/(.*)$', admin.site.root),
)

10) Write an aemin definition for the objects
from ana.models import Event
from django.contrib import admin

class EventAdmin(admin.ModelAdmin):
# fieldsets = [
# (None, {'fields': ['question']}),
# ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
# ]

# display these fields in the listing
list_display = ("name", "start", "end")

# add filter to the right of a listing
#list_filter = ['name']

# Let's add some search capability:
search_fields = ['name']

# Finally, because Poll objects have dates, it'd be convenient
# to be able to drill down by date. Add this line:
# date_hierarchy = 'pub_date'

# liste verkleinern
# def queryset(self, request):
# return self.model._default_manager.filter(ocean=0)

admin.site.register(Event, EventAdmin)

6 Jan 2009

Django

As everyone I became infected by Python lately. Django seems to be a nice web development framework which eases the pain of everyday tasks quiet nicely.

Luckily «The Django Book» is freely available in HTML. However, I hate going through the TOC over and over again, an RSS Feed in Firefox's Bookmark toolbar saves me 1-2 clicks for every lookup.



Here ist the TOC RSS.