Tyler Harms / @harmstyler
Guido van Rossum
spam & eggs
Significant White Space
import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
...
$ pip install django
$ django-admin.py startproject sdcc
sdcc
├── manage.py
└── sdcc
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
$ manage.py <command> [options]
$ python manage.py syncdb
Creating tables ... Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_groups Creating table auth_user_user_permissions Creating table auth_user Creating table django_content_type Creating table django_session Creating table django_site
$ python manage.py runserver
Validating models...
0 errors found
November 09, 2013 - 13:29:07
Django version 1.5.5, using settings 'sdcc.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
$ python manage.py startapp campspeakers
├── campspeakers
│ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── manage.py
└── sdcc
├── __init__.py
├── settings.py
├── urls.py
├── wsgi.py
Weird nameing convention, but follows M-V-C design pattern.
Model = Model (ORM)from django.db import models
class Speaker(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
class Meta:
ordering = ['last_name']
def _get_full_name(self):
return '%s %s' % (self.first_name, self.last_name)
full_name = property(_get_full_name)
def __unicode__(self):
return self.full_name
from django.conf.urls 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'^$', 'sdcc.views.home', name='home'),
# url(r'^sdcc/', include('sdcc.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)),
)
from django.contrib import admin
from .models import Speaker
admin.site.register(Speaker)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
from django.shortcuts import render
from django.http import Http404
from django import template
from .models import Speaker
def index(request):
try:
speaker_list = Speaker.objects.all()
except Speaker.DoesNotExist:
raise Http404
return render(request, 'campspeakers/speaker_list.html',
{'speaker_list': speaker_list})
from django.views.generic import TemplateView
class IndexView(TemplateView):
template_name = "campspeakers/index.html"
from django.views.generic import ListView
from .models import Speaker
class BookListView(ListView):
model = Speaker
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super(BookListView, self).get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['speaker_list'] = Speaker.objects.all()
return context
from django.conf.urls 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'^$', 'sdcc.views.home', name='home'),
# url(r'^sdcc/', include('sdcc.foo.urls')),
url(r'^speakers/', include('campspeakers.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)),
)
from django.conf.urls import patterns, url
from .views import BookListView
import views
urlpatterns = patterns('',
(r'^list$', BookListView.as_view()),
url(r'^$', views.index, name='index'),
)
{% block content %}{% endblock %}
{{ speaker }}
"This is South, intelligent schema and data migrations for Django projects"
Replaces syncdb.
$ python manage.py schemamigration campspeakers --initial
$ python manage.py migrate campspeakers
import os
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_DIR = os.path.abspath(os.path.join(PROJECT_DIR, '../../'))