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

Django introduction workshop

Django introduction workshop

Django introduction workshop, level: beginner

Sander van de Graaf

February 01, 2012
Tweet

More Decks by Sander van de Graaf

Other Decks in Technology

Transcript

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

    • Modules (apps) • Admin • Models • Templates
  2. “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
  3. Key philosophies • Loose coupling • Less code • Quick

    development • DRY • Explicit is better than implicit • Consistency
  4. 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
  5. 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
  6. 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
  7. DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'example.sql', 'USER':

    '', 'PASSWORD': '', 'HOST': '', 'PORT': '', } } db settings settings.py
  8. 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
  9. 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
  10. 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) )
  11. 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