Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django introduction workshop

Django introduction workshop

Django introduction workshop, level: beginner

D818fa99a5fae8aae7102277734e6fe7?s=128

Sander van de Graaf

February 01, 2012
Tweet

More Decks by Sander van de Graaf

Other Decks in Technology

Transcript

  1. Introductie

  2. Agenda • Historie • Architectuur • Start project • Views

    • Modules (apps) • Admin • Models • Templates
  3. Historie

  4. Begonnen in 2003

  5. Lawrence, KS

  6. Lawrence Journal

  7. Sinds 2005 open source

  8. Django Reinhardt 1910-1953

  9. None
  10. None
  11. >= 2.5

  12. Google App Engine

  13. “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
  14. Architectuur

  15. MVC Framework

  16. None
  17. Key philosophies

  18. Key philosophies • Loose coupling • Less code • Quick

    development • DRY • Explicit is better than implicit • Consistency
  19. Project

  20. git init GIT

  21. virtualenv . Virtual environment opzetten

  22. django-admin.py startproject [app] Start project

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

  24. Profit! $$!

  25. python manage.py startapp articles module maken

  26. None
  27. 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
  28. view maken views.py from django.http import HttpResponse def hello(request): return

    HttpResponse("Hello world")
  29. 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
  30. python [app]/manage.py runserver dev server starten

  31. Admin

  32. • admin app enablen • database settings toevoegen • urls

    aanpassen • superuser aanmaken
  33. 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
  34. DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'example.sql', 'USER':

    '', 'PASSWORD': '', 'HOST': '', 'PORT': '', } } db settings settings.py
  35. python manage.py syncdb db settings + user

  36. 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
  37. python [app]/manage.py runserver dev server starten

  38. http://127.0.0.1:8000/admin admin openen

  39. Models!

  40. None
  41. 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
  42. python [app]/manage.py syncdb db syncen

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

    aan admin articles/admin.py
  44. python [app]/manage.py runserver dev server starten

  45. http://127.0.0.1:8000/admin admin openen

  46. article aanmaken

  47. Templates

  48. 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) )
  49. 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<id>[0-9]+)$', 'articles.views.article', name='articleView'), ) url enablen urls.py
  50. import os PROJECT_PATH = os.path.realpath(os.path.dirname(__file__)) TEMPLATE_DIRS = ( PROJECT_PATH +

    '/templates/', ) template path setten settings.py
  51. <h1>{{article.title}}</h1> <p>{{article.intro}}</p> <p>{{article.body}}</p> template maken templates/view.html

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

  53. done!