Django kann auch ohne Datenbank benutzt werden, das objektrelational Mapping (ORM) ist jedoch eines seiner Stärken.
Beispiel:
class Reporter(models.Model):
full_name = models.CharField(max_length=70)
def __unicode__(self):
return self.full_name
class Article(models.Model):
pub_date = models.DateTimeField()
headline = models.CharField(max_length=200)
content = models.TextField()
reporter = models.ForeignKey(Reporter)
def __unicode__(self):
return self.headline
Models Doku: http://docs.djangoproject.com/en/dev/topics/db/models/
Die API ist direkt verfügbar:
>>> from mysite.models import Reporter, Article
# Alle Zeilen zurückgeben, noch keine vorhanden
>>> Reporter.objects.all() SQL: SELECT .... FROM ...
[]
# Neuen Reporter erstellen
>>> r = Reporter(full_name='John Smith')
# Objekt speichern --> SQL: INSERT INTO ....
>>> r.save()
# Now it has an ID: Primärschlüssel
>>> r.id
1
# Jetzt ist die Tabelle nicht mehr leer.
>>> Reporter.objects.all()
[<Reporter: John Smith>]
# DB Spalten werden Attribute:
>>> r.full_name
'John Smith'
# Django provides a rich database lookup API.
>>> Reporter.objects.get(id=1)
<Reporter: John Smith>
>>> Reporter.objects.get(full_name__startswith='John')
<Reporter: John Smith>
>>> Reporter.objects.get(full_name__contains='mith')
<Reporter: John Smith>
>>> Reporter.objects.get(id=2)
Traceback (most recent call last):
...
DoesNotExist: Reporter matching query does not exist.
# Create an article.
>>> from datetime import datetime
>>> a = Article(pub_date=datetime.now(), headline='Django is cool',
... content='Yeah.', reporter=r)
>>> a.save()
# Article objects get API access to related Reporter objects.
>>> r = a.reporter
>>> r.full_name
'John Smith'
# And vice versa: Reporter objects get API access to Article objects.
>>> r.article_set.all()
[<Article: Django is cool>]
# The API follows relationships as far as you need, performing efficient
# JOINs for you behind the scenes.
# This finds all articles by a reporter whose name starts with "John".
>>> Article.objects.filter(reporter__full_name__startswith="John")
[<Article: Django is cool>]
# Change an object by altering its attributes and calling save().
>>> r.full_name = 'Billy Goat'
>>> r.save()
# Delete an object with delete().
>>> r.delete()
Query API: http://docs.djangoproject.com/en/dev/topics/db/queries/
Das contrib-Modul “admin” ermöglicht es die Tabellen zu bearbeiten (INSERT, UPDATE, DELETE):
# In admin.py in the same directory...
import models
from django.contrib import admin
admin.site.register(models.Article)
Schöne URLs: ohne .php
oder .asp
auch nicht /cgi-bin/edit?article=123
.
Beispiel urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^articles/(\d{4})/$', 'mysite.views.year_archive'),
(r'^articles/(\d{4})/(\d{2})/$', 'mysite.views.month_archive'),
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'mysite.views.article_detail'),
)
reverse()
erstellt werden.{% extends "base.html" %}
{% block title %}Articles for {{ year }}{% endblock %}
{% block content %}
<h1>Articles for {{ year }}</h1>
{% for article in article_list %}
<p>{{ article.headline }}</p>
<p>By {{ article.reporter.full_name }}</p>
<p>Published {{ article.pub_date|date:"F j, Y" }}</p>
{% endfor %}
{% endblock %}
Templatesprache unterstützt Vererbung: Nur gewisse Blöcke des Eltern-Templates werden ersetzt, der Rest wird übernommen.
Hinweis: Ich verwende die Templatesprache kaum.
und es gibt noch viel mehr...