Sie sind auf Seite 1von 85

The Web framework

for perfectionists with deadlines


Markus Zapke-Gründemann
Leipzig on Rails 15.03.2011
Übersicht
• Über mich • Django in
Deutschland
• Was ist Django?
• Weiterführende Links
• Python
• DjangoCon Europe
• Architektur
• Django in freier
Wildbahn
Markus
Zapke-Gründemann
• Softwareentwickler seit 2001
• Softwareentwicklung mit Python, Django
und Mercurial
• Freier Softwareentwickler und Berater seit
2008
• www.keimlink.de
Was ist Django?
Django Reinhardt
* 23.1.1910
✝ 16.5.1953

Bildquelle: http://en.wikipedia.org/wiki/File:Django_Reinhardt_%28Gottlieb_07301%29.jpg
Was ist Django?
• Web Application Framework
• In Python geschrieben
• Open Source Software (BSD Lizenz)
• Django Software Foundation
• Umfangreiche Dokumentation
• Große, freundliche Community
Was ist Django?
• Rapid Development
• Loose Coupling
• Wiederverwendbare Applikationen
• Don't Repeat Yourself (DRY)
Every piece of knowledge must have a single,
unambiguous, authoritative representation within a system.
http://c2.com/cgi/wiki?DontRepeatYourself
Und natürlich Ponies!
Ponies?
Ponies?
The Python logo is a trademark of the Python Software Foundation.
Guido van Rossum
begann 1989 mit
der Entwicklung
von Python

Bildquelle: http://commons.wikimedia.org/wiki/File:Guido_van_Rossum_OSCON_2006.jpg
class Input(Widget):
"""
Base class for all <input> widgets (except type='checkbox' and
type='radio', which are special).
"""
input_type = None # Subclasses must define this.

def render(self, name, value, attrs=None):


if value is None: value = ''
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
if value != '':
# Only add the 'value' attribute if a value is non-empty.
final_attrs['value'] = force_unicode(formats.localize_input(value))
return mark_safe(u'<input%s />' % flatatt(final_attrs))

Objektorientierte Sprache
Große
Standardbibliothek
Bildquelle: http://www.flickr.com/photos/jhoweaa/327651705/
Unit Testing

Bildquelle: http://www.flickr.com/photos/sidelong/246816211/
Alle Logos und Warenzeichen auf dieser Seite sind Eigentum der jeweiligen Besitzer und Lizenzhalter.
Gute Werkzeuge
für Programmierer

Bildquelle: http://www.flickr.com/photos/philentropist/176054470/
Freie Software

Bildquelle: http://www.flickr.com/photos/gagilas/3809232008/
FastCGI
WSGI

Bildquelle: http://www.flickr.com/photos/torkildr/3462607995/
Architektur
Full Stack Framework
Full Stack Framework

Python
Full Stack Framework

Datenbanken Python
Full Stack Framework

Unicode

Datenbanken Python
Full Stack Framework

Models

Unicode

Datenbanken Python
Full Stack Framework

Object Relational
Mapper

Models

Unicode

Datenbanken Python
Full Stack Framework

Object Relational
Mapper

Models Applications

Unicode

Datenbanken Python
Full Stack Framework

Object Relational
Mapper

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework

Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework

Template

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags

Template

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter

Template

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter

Template Serializer

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter XML

Template Serializer

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter XML JSON

Template Serializer

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter XML JSON YAML

Template Serializer

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter XML JSON YAML

Template Serializer Syndication

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter XML JSON YAML RSS

Template Serializer Syndication

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter XML JSON YAML RSS Atom

Template Serializer Syndication

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter XML JSON YAML RSS Atom

Formulare
Template Serializer Syndication

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter XML JSON YAML RSS Atom

Validatoren

Formulare
Template Serializer Syndication

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter XML JSON YAML RSS Atom
Testing

Validatoren

Formulare
Template Serializer Syndication

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter XML JSON YAML RSS Atom
Testing
Cache

Validatoren

Formulare
Template Serializer Syndication

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Tags Filter XML JSON YAML RSS Atom
Testing
Cache
i18n

Validatoren

Formulare
Template Serializer Syndication

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Full Stack Framework
Middleware

Tags Filter XML JSON YAML RSS Atom


Testing
Cache
i18n

Validatoren

Formulare
Template Serializer Syndication

View
Object Relational
Mapper URLConf

Models Applications Admin

Unicode

Datenbanken Python
Webserver
Webserver
Webserver

URLConf
Webserver

URLConf Middleware
Webserver

URLConf Middleware
Webserver

URLConf Middleware

View
Webserver

URLConf Middleware

View
Webserver

URLConf Middleware

View

Model (ORM)
Webserver

URLConf Middleware

View

Model (ORM)
Webserver

URLConf Middleware

View

Model (ORM)

Datenbank
Webserver

URLConf Middleware

View

Model (ORM)

Datenbank
Webserver

URLConf Middleware

View

Template

Model (ORM)

Datenbank
Webserver

URLConf Middleware

View

Template

Model (ORM)

Datenbank
Webserver

URLConf Middleware

View

Template

Model (ORM)

Tags & Filter

Datenbank
Webserver

URLConf Middleware

View

Template

Model (ORM)

Tags & Filter

Datenbank
$ django-admin.py startproject project

$ tree project/
project/
|-- __init__.py
|-- manage.py
Projekt
|-- settings.py
`-- urls.py

$ cd project

$ python manage.py startapp ponyfarm

$ tree
.
|-- __init__.py
|-- manage.py
|-- ponyfarm
| |-- __init__.py
| |-- models.py
| |-- tests.py
| `-- views.py
|-- settings.py
`-- urls.py
project/ponyfarm/models.py
from django.db import models
from django.contrib.auth.models import User

class Farm(models.Model):
title = models.CharField(max_length=100)
description = models.TextField(blank=True)

class Meta:
pass

@models.permalink
def get_absolute_url(self):
Model

return ('ponyfarm.views.farm', (), {'farm_id': self.id})

def __unicode__(self):
return self.title

class Pony(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
birthday = models.DateField()
farm = models.ManyToManyField(Farm)
user = models.ForeignKey(User)

class Meta:
pass

def farm_list(self):
return ', '.join([farm.title for farm in self.farm.all()])
farm_list.short_description = u'List of farms'

@models.permalink
def get_absolute_url(self):
return ('ponyfarm_object_detail', (), {'slug': self.slug})

def __unicode__(self):
return self.title
project/urls.py
from django.conf.urls.defaults import patterns, include, url

from django.contrib import admin


admin.autodiscover()

urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
URLConf
(r'^', include('ponyfarm.urls'))
)

project/ponyfarm/urls.py
from django.conf.urls.defaults import patterns, include, url

from ponyfarm.models import Pony

pony_info_dict = {'queryset': Pony.objects.all()}

urlpatterns = patterns('ponyfarm.views',
(r'^farm/(?P<farm_id>[\d]+)/$', 'farm')
)
urlpatterns += patterns('django.views.generic.list_detail',
url(r'^pony/(?P<slug>[-\w]+)/$', 'object_detail', pony_info_dict,
'ponyfarm_object_detail'),
url(r'^$', 'object_list', pony_info_dict, 'ponyfarm_object_list')
)
project/ponyfarm/views.py
View

from django.shortcuts import get_object_or_404, render_to_response

from ponyfarm.models import Farm

def farm(request, farm_id):


farm = get_object_or_404(Farm, pk=farm_id)
return render_to_response('ponyfarm/farm.html', {'farm': farm})
project/templates/base.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>{% block title %}My Pony Farm{% endblock %}</title>
</head>
Template
<body>
<h2>My Pony Farm</h2>
{% block content %}
{% endblock %}
</body>
</html>

project/ponyfarm/templates/ponyfarm/farm.html
{% extends "base.html" %}
{% block title %}{{ block.super }} - {{ farm.title }}{% endblock %}
{% block content %}
<h3>{{ farm.title }}</h3>
<p>{{ farm.description|linebreaks }}</a></p>
<p><a href="{% url ponyfarm_edit_farm farm_id=farm.id %}">edit</a></p>
<p><a href="{% url ponyfarm_object_list %}">back to overview</a></p>
{% endblock %}
project/ponyfarm/admin.py
from django.contrib import admin
from ponyfarm.models import Farm, Pony
Admin

class FarmAdmin(admin.ModelAdmin):
pass

class PonyAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ['title']}
date_hierarchy = 'birthday'
list_display = ('title', 'birthday', 'farm_list')
list_filter = ('farm',)
search_fields = ('title',)

admin.site.register(Farm, FarmAdmin)
admin.site.register(Pony, PonyAdmin)
Admin
Eingebauter Webserver
$ python manage.py runserver
Validating models...
0 errors found

Django version 1.2.5, using settings 'ponysite.settings'


Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[25/Mar/2010 17:48:50] "GET /admin/ HTTP/1.1" 200 1686
[25/Mar/2010 17:49:04] "POST /admin/ HTTP/1.1" 302 0
[25/Mar/2010 17:49:04] "GET /admin/ HTTP/1.1" 200 5674
[25/Mar/2010 17:49:08] "GET /admin/auth/user/ HTTP/1.1" 200 4788
[25/Mar/2010 17:49:14] "GET /admin/ HTTP/1.1" 200 5674
[25/Mar/2010 17:49:17] "GET /admin/auth/user/ HTTP/1.1" 200 4788
Django
in freier Wildbahn
www.ljworld.com
www.theonion.com
Bildquelle: http://twitter.com/TheOnion/status/10921296161
www.everyblock.com
disqus.com
www.rapidsms.org
Bildquelle: http://www.rapidsms.org/about/take-the-tour/
filmaster.com
trailmapping.com
Andere Django-Nutzer

• National Geographic
• Discovery Channel
• Instagram
• Mozilla Add-ons
• FreeNAS
Django
in Deutschland
Django-Entwickler
Bildquelle: http://djangopeople.net/de/
Deutscher
Django-Verein e.V.
• Gegründet im Dezember 2009
• Kommunikation der Entwickler und
Anwender fördern
• Weiterentwicklung von Django voran
treiben
• www.django-de.org
Weiterführende Links
• www.djangoproject.com
• www.djangobook.com/en/2.0
• www.django-workshop.de
• www.djangopackages.com
• www.djangosnippets.org
• www.python.org
• www.diveintopython.org
• learnpythonthehardway.org
djangocon.eu
Lizenz
Dieses Werk ist unter einem Creative Commons
Namensnennung-Weitergabe unter gleichen
Bedingungen 3.0 Unported Lizenzvertrag lizenziert.
Um die Lizenz anzusehen, gehen Sie bitte zu
http://creativecommons.org/licenses/by-sa/3.0/ oder
schicken Sie einen Brief an Creative Commons, 171
Second Street, Suite 300, San Francisco, California
94105, USA.

Django is a registered trademark of the Django Software Foundation.