The Web framework for perfectionists with deadlines

Markus Zapke-Gründemann Python User Group München 22.03.2011

Übersicht
• Über mich • Was ist Django? • Architektur • Nützliche Django Apps • Django in freier Wildbahn • Django in Deutschland • Weiterführende Links • DjangoCon Europe

Markus Zapke-Gründemann
• Softwareentwickler seit 2001 • Softwareentwicklung mit Python, Django
und Mercurial 2008

• Freier Softwareentwickler und Berater seit • 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?

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 Unicode Datenbanken Python Admin

Full Stack Framework

Object Relational Mapper Models Applications Unicode Datenbanken Python

URLConf Admin

Full Stack Framework
View URLConf Applications Unicode Datenbanken Python Admin

Object Relational Mapper Models

Full Stack Framework
Template Object Relational Mapper Models Applications Unicode Datenbanken Python View URLConf Admin

Full Stack Framework
Tags Template Object Relational Mapper Models Applications Unicode Datenbanken Python View URLConf Admin

Full Stack Framework
Tags Filter Template Object Relational Mapper Models Applications Unicode Datenbanken Python View URLConf Admin

Full Stack Framework
Tags Filter Template Object Relational Mapper Models Applications Unicode Datenbanken Python Serializer View URLConf Admin

Full Stack Framework
Tags Filter XML Template Object Relational Mapper Models Applications Unicode Datenbanken Python Serializer View URLConf Admin

Full Stack Framework
Tags Filter XML JSON Template Object Relational Mapper Models Applications Unicode Datenbanken Python Serializer View URLConf Admin

Full Stack Framework
Tags Filter XML JSON YAML Template Object Relational Mapper Models Applications Unicode Datenbanken Python Serializer View URLConf Admin

Full Stack Framework
Tags Filter XML JSON YAML Template Object Relational Mapper Models Applications Unicode Datenbanken Python Serializer View URLConf Admin Syndication

Full Stack Framework
Tags Filter XML JSON YAML RSS Template Object Relational Mapper Models Applications Unicode Datenbanken Python Serializer View URLConf Admin Syndication

Full Stack Framework
Tags Filter XML JSON YAML RSS Atom Template Object Relational Mapper Models Applications Unicode Datenbanken Python Serializer View URLConf Admin Syndication

Full Stack Framework
Tags Filter XML JSON YAML RSS Atom Formulare Template Serializer View URLConf Applications Unicode Datenbanken Python Admin Syndication

Object Relational Mapper Models

Full Stack Framework
Tags Filter XML JSON YAML RSS Atom Validatoren Formulare Template Serializer View URLConf Applications Unicode Datenbanken Python Admin Syndication

Object Relational Mapper Models

Full Stack Framework
Testing Tags Filter XML JSON YAML RSS Atom Validatoren Formulare Template Serializer View URLConf Applications Unicode Datenbanken Python Admin Syndication

Object Relational Mapper Models

Full Stack Framework
Testing Cache Tags Filter XML JSON YAML RSS Atom Validatoren Formulare Template Serializer View URLConf Applications Unicode Datenbanken Python Admin Syndication

Object Relational Mapper Models

Full Stack Framework
Testing Cache Tags Filter XML JSON YAML RSS Atom Validatoren Formulare Template Serializer View URLConf Applications Unicode Datenbanken Python Admin Syndication i18n

Object Relational Mapper Models

Full Stack Framework
Middleware Testing Cache Tags Filter XML JSON YAML RSS Atom Validatoren Formulare Template Serializer View URLConf Applications Unicode Datenbanken Python Admin Syndication i18n

Object Relational Mapper Models

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 |-- 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

Projekt

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): 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

Model

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)), (r'^', include('ponyfarm.urls')) )

URLConf

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') )

View

project/ponyfarm/views.py
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> <body> <h2>My Pony Farm</h2> {% block content %} {% endblock %} </body> </html>

Template

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

Nützliche Django Apps

Django Debug Toolbar

http://robhudson.github.com/django-debug-toolbar/

Django Debug Toolbar

http://robhudson.github.com/django-debug-toolbar/

Django Debug Toolbar

http://robhudson.github.com/django-debug-toolbar/

Schema and Data Migrations
http://south.aeracode.org/

Distributed Task Queue
http://celeryproject.org/

Modular Search
http://haystacksearch.org/

WSGI HTTP Server for UNIX
http://gunicorn.org/

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 treiben

• Weiterentwicklung von Django voran • www.django-de.org

• 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

Weiterführende Links

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.

Sign up to vote on this title
UsefulNot useful