Slide 1

Slide 1 text

Introductie

Slide 2

Slide 2 text

Agenda • Historie • Architectuur • Start project • Views • Modules (apps) • Admin • Models • Templates

Slide 3

Slide 3 text

Historie

Slide 4

Slide 4 text

Begonnen in 2003

Slide 5

Slide 5 text

Lawrence, KS

Slide 6

Slide 6 text

Lawrence Journal

Slide 7

Slide 7 text

Sinds 2005 open source

Slide 8

Slide 8 text

Django Reinhardt 1910-1953

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

>= 2.5

Slide 12

Slide 12 text

Google App Engine

Slide 13

Slide 13 text

“My personal favorite -- and I expect that that will remain a personal favorite for a long time -- is something named Django. ... I highly recommend it.” -- Guido van Rossum

Slide 14

Slide 14 text

Architectuur

Slide 15

Slide 15 text

MVC Framework

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Key philosophies

Slide 18

Slide 18 text

Key philosophies • Loose coupling • Less code • Quick development • DRY • Explicit is better than implicit • Consistency

Slide 19

Slide 19 text

Project

Slide 20

Slide 20 text

git init GIT

Slide 21

Slide 21 text

virtualenv . Virtual environment opzetten

Slide 22

Slide 22 text

django-admin.py startproject [app] Start project

Slide 23

Slide 23 text

python [app]/manage.py runserver dev server starten

Slide 24

Slide 24 text

Profit! $$!

Slide 25

Slide 25 text

python manage.py startapp articles module maken

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'articles', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', ) app enablen settings.py

Slide 28

Slide 28 text

view maken views.py from django.http import HttpResponse def hello(request): return HttpResponse("Hello world")

Slide 29

Slide 29 text

from django.conf.urls.defaults import patterns, include, url # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'foobar.views.home', name='home'), # url(r'^foobar/', include('foobar.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: # url(r'^admin/', include(admin.site.urls)), url(r'^$', 'articles.views.hello', name='home'), ) url toekennen urls.py

Slide 30

Slide 30 text

python [app]/manage.py runserver dev server starten

Slide 31

Slide 31 text

Admin

Slide 32

Slide 32 text

• admin app enablen • database settings toevoegen • urls aanpassen • superuser aanmaken

Slide 33

Slide 33 text

INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'articles', # Uncomment the next line to enable the admin: 'django.contrib.admin', # Uncomment the next line to enable admin documentation: 'django.contrib.admindocs', ) admin app enablen settings.py

Slide 34

Slide 34 text

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'example.sql', 'USER': '', 'PASSWORD': '', 'HOST': '', 'PORT': '', } } db settings settings.py

Slide 35

Slide 35 text

python manage.py syncdb db settings + user

Slide 36

Slide 36 text

from django.conf.urls.defaults import patterns, include, url # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'foobar.views.home', name='home'), # url(r'^foobar/', include('foobar.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)), url(r'^$', 'articles.views.hello', name='home'), ) url enablen urls.py

Slide 37

Slide 37 text

python [app]/manage.py runserver dev server starten

Slide 38

Slide 38 text

http://127.0.0.1:8000/admin admin openen

Slide 39

Slide 39 text

Models!

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

from django.db import models from datetime import * class Article(models.Model): created_at = models.DateTimeField(auto_now_add=True, default=datetime.now()) updated_at = models.DateTimeField(auto_now=True) publish_at = models.DateTimeField(default=datetime.now()) title = models.CharField(max_length=255) intro = models.TextField(null=False) body = models.TextField(null=False) article model models.py

Slide 42

Slide 42 text

python [app]/manage.py syncdb db syncen

Slide 43

Slide 43 text

from django.contrib import admin from app.articles.models import * admin.site.register(Article) toevoegen aan admin articles/admin.py

Slide 44

Slide 44 text

python [app]/manage.py runserver dev server starten

Slide 45

Slide 45 text

http://127.0.0.1:8000/admin admin openen

Slide 46

Slide 46 text

article aanmaken

Slide 47

Slide 47 text

Templates

Slide 48

Slide 48 text

view wijzigen views.py from django.http import HttpResponse from django.shortcuts import render_to_response from articles.models import * def hello(request): return HttpResponse("Hello world") def article(request,id): article = Article.objects.get(pk=id) return render_to_response( 'view.html', locals(), context_instance=RequestContext(request) )

Slide 49

Slide 49 text

from django.conf.urls.defaults import patterns, include, url # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'foobar.views.home', name='home'), # url(r'^foobar/', include('foobar.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: url(r'^admin/', include(admin.site.urls)), url(r'^$', 'articles.views.hello', name='home'), url(r'^article/(?P[0-9]+)$', 'articles.views.article', name='articleView'), ) url enablen urls.py

Slide 50

Slide 50 text

import os PROJECT_PATH = os.path.realpath(os.path.dirname(__file__)) TEMPLATE_DIRS = ( PROJECT_PATH + '/templates/', ) template path setten settings.py

Slide 51

Slide 51 text

{{article.title}}

{{article.intro}}

{{article.body}}

template maken templates/view.html

Slide 52

Slide 52 text

http://127.0.0.1:8000/article/1 url openen

Slide 53

Slide 53 text

done!