Writing your first Django app, part 1

Original: http://docs.djangoproject.com/en/dev/intro/tutorial01/

Beispiel: Umfrage (Poll) mit Stimmen (Votes)

Creating a project

Project: DB-Config, Installierte Applikationen, keine Models!

django-admin.py startproject mysite

Erstellt:

mysite/
    __init__.py
    manage.py
    settings.py
    urls.py

The development server

python manage.py runserver:

Validating models...
0 errors found.

Django version 1.0, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

firefox http://127.0.0.1:8000/

Database setup

Edit settings.py.

DATABASE_ENGINE (Tipp: postgresql_psycopg2 oder sqlite3), DATABASE_NAME, DATABASE_USER ...

INSTALLED_APPS:

  • django.contrib.auth – User, Group, Permission ...
  • django.contrib.sessions
python manage.py syncdb

Entsprechend den Models der installlierten Applikationen werden Tabellen in der DB erstellt.

Creating models

python manage.py startapp polls

Erstellt:

polls/
__init__.py models.py views.py

Edit polls/models.py:

from django.db import models

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

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

Klasse –> Tabelle

Attribut (Field) –> Spalte einer Tabelle

Objekt/Instanz –> Zeile einer Tabelle

Edit settings.py, ‘mysite.polls’ einbinden:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'mysite.polls'
)
python manage.py syncdb

... Tabellen werden erstellt.

Playing with the API

python manage.py shell

Database API kennenlernen:

>>> from mysite.polls.models import Poll, Choice # Import the model classes we just wrote.

# No polls are in the system yet.
>>> Poll.objects.all()
[]

# Create a new Poll.
>>> import datetime
>>> p = Poll(question="What's up?", pub_date=datetime.datetime.now())

# Save the object into the database. You have to call save() explicitly.
>>> p.save()

# Now it has an ID.
>>> p.id
1

# Access database columns via Python attributes.
>>> p.question
"What's up?"
>>> p.pub_date
datetime.datetime(2007, 7, 15, 12, 00, 53)

# Change values by changing the attributes, then calling save().
>>> p.pub_date = datetime.datetime(2007, 4, 1, 0, 0)
>>> p.save()

# objects.all() displays all the polls in the database.
>>> Poll.objects.all()
[<Poll: Poll object>]

Halt. <Poll: Poll object> ist nicht aussagekräftig:

# Datei models.py
class Poll(models.Model):
    # ...
    def __unicode__(self):
        return self.question

class Choice(models.Model):
    # ...
    def __unicode__(self):
        return self.choice

Methoden hinzufügen:

# Datei models.py
import datetime

class Poll(models.Model):
    # ...
    def was_published_today(self):
        return self.pub_date.date() == datetime.date.today()

Previous topic

Django at a glance

Next topic

Part 2: Admin Site