unchain your web development with django

33
Unchain Your Web Development with Django The web framework for perfectionists with deadlines.

Upload: joey-wilhelm

Post on 13-Apr-2017

260 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Unchain Your Web Development With Django

Unchain Your Web Development with Django

The web framework for perfectionists with deadlines.

Page 2: Unchain Your Web Development With Django

Who am I?

● Started using Python 1.5.2 in 1998

● Started using Django 0.96 in 2008

● Contributing to open source projects since ~2000

● @mrbeersnob

● github.com/tarkatronic

Page 3: Unchain Your Web Development With Django

What is Django?

Page 4: Unchain Your Web Development With Django

No, really, what is it?

The web framework for professionals with deadlines

● Originally built for the Lawrence Journal-World newspaper in 2003

● A bundled collection of tools to build powerful web applications quickly

● Focused on automation and DRY

Page 5: Unchain Your Web Development With Django

Who actually uses this thing?

● Disqus

● Instagram

● Pinterest

● Washington Post

● National Geographic

● Many, many more…

● https://www.djangosites.org/

Page 6: Unchain Your Web Development With Django

Why should I use it?

● ORM

● Caching

● Internationalization

● Class-based views!

● Templating

● Automatically generated admin interface

● Database migrations

● Built-in management commands

Page 7: Unchain Your Web Development With Django

Django doesn’t provide ____. Do I have to build it?

● Social authentication? django-allauth

● REST API? django-rest-framework

● Two factor authentication? django-two-factor-auth

● CMS? wagtail, django-cms, etc

● https://www.djangopackages.com/

● https://djangosnippets.org/

Page 8: Unchain Your Web Development With Django

Let’s get started!

Page 9: Unchain Your Web Development With Django

Setting things up

● pip install Django

● django-admin.py startproject cocktails

● …

● Profit!

Page 10: Unchain Your Web Development With Django

Okay, maybe a little more than that../manage.py runserver…Starting development server at http://127.0.0.1:8000/

Page 11: Unchain Your Web Development With Django

Creating an application

django-admin.py startapp recipes

recipes/migrations/

__init__.py__init__.pyadmin.pymodels.pytests.pyviews.py

settings.py:INSTALLED_APPS = (

…'cocktails.recipes'

)

Page 12: Unchain Your Web Development With Django

First step: Modelsfrom django.db import models

class Ingredient(models.Model): OUNCE = 'ounce' TEASPOON = 'tsp' TABLESPOON = 'tbsp' DASH = 'dash'

MEASUREMENTS = ( (OUNCE, 'Ounce(s)'), (TEASPOON, 'Teaspoon(s)'), (TABLESPOON, 'Tablespoon(s)'), (DASH, 'Dash(es)'), ) name = models.CharField(max_length=255) measurement = models.CharField(max_length=5, choices=MEASUREMENTS, null=True, blank=True)

def __str__(self): return self.name

Page 13: Unchain Your Web Development With Django

A couple more...class Drink(models.Model): name = models.CharField(max_length=255) components = models.ManyToManyField('Ingredient', through='Component', related_name='drinks')

def __str__(self): return self.name

class Component(models.Model): drink = models.ForeignKey('recipes.Drink', related_name='+') ingredient = models.ForeignKey('recipes.Ingredient', related_name='+') amount = models.FloatField()

def __str__(self): return '%s %s %s (%s)' % (self.ingredient.name, self.amount, self.ingredient.get_measurement_display() or '', self.drink.name)

Page 14: Unchain Your Web Development With Django

One more piececlass Step(models.Model): drink = models.ForeignKey('Drink', related_name='steps') text = models.TextField()

class Meta: order_with_respect_to = 'drink'

def __str__(self): return '%s step #%s' % (self.drink.name, self._order + 1)

Page 15: Unchain Your Web Development With Django

Time to set up the database...

CREATE DATABASE;CREATE TABLE …;

...right?

Page 16: Unchain Your Web Development With Django

Nope! Migrations to the rescue../manage.py makemigrationsMigrations for 'recipes': 0001_initial.py: - Create model Component - Create model Drink - Create model Ingredient - Create model Step - Add field components to drink - Add field drink to component - Add field ingredient to component - Set order_with_respect_to on step to drink

./manage.py migrateOperations to perform: Synchronize unmigrated apps: staticfiles, messages Apply all migrations: admin, contenttypes, recipes, auth, sessions … lots more words …

Done!

Page 17: Unchain Your Web Development With Django

About that admin interface...

admin.py:from django.contrib import adminfrom .models import Component, Drink, Ingredient, Step

class ComponentInline(admin.TabularInline): model = Component

class StepInline(admin.StackedInline): model = Step

class DrinkAdmin(admin.ModelAdmin): inlines = [ComponentInline, StepInline]

admin.site.register(Component)admin.site.register(Drink)admin.site.register(Ingredient)admin.site.register(Step)

Page 18: Unchain Your Web Development With Django

Creating an admin user

Another built-in management command!

./manage.py createsuperuserUsername (leave blank to use 'jwilhelm'):Email address: [email protected]:Password (again):Superuser created successfully.

Page 19: Unchain Your Web Development With Django

Log in, and like magic, we get...

Page 20: Unchain Your Web Development With Django

Adding some records

Page 21: Unchain Your Web Development With Django

Our inlines at work

Page 22: Unchain Your Web Development With Django

Making things visible: views

views.py:from django.core.urlresolvers import reverse_lazyfrom django.views.generic import DetailView, ListView

from .models import Component, Drink

class DrinkListView(ListView): model = Drink

class DrinkDetailView(DetailView): model = Drink

def get_context_data(self, **kwargs): context = super(DrinkDetailView, self).get_context_data(**kwargs) context.update({'components': Component.objects.filter(drink=self.get_object())}) return context

Page 23: Unchain Your Web Development With Django

Pulling it together with a couple templates

base.html:

<html> <head> <title>Cocktail Database</title> </head> <body> {% block content %}{% endblock %} </body></html>

Page 24: Unchain Your Web Development With Django

List all the things!

recipes/drink_list.html:

{% extends "base.html" %}

{% block content %} {% for drink in object_list %} {% if forloop.first %} <ul> {% endif %} <li><a href="{% url 'drink_detail' drink.id %}">{{ drink.name }}</a></li> {% if forloop.last %} </ul> {% endif %} {% empty %} No drinks yet in the database. {% endfor %}{% endblock %}

Page 25: Unchain Your Web Development With Django

It's all in the details

recipes/drink_detail.html:

{% extends "base.html" %}

{% block content %} <h1>{{ drink.name }}</h1> <dl> <dt>Ingredients</dt> <dd> <ul> {% for component in components %} <li>{{ component.amount }}{% if component.ingredient.measurement %} {{ component.ingredient.measurement }}{% endif %} {{ component.ingredient.name }}</li> {% endfor %} </ul> </dd>...

Page 26: Unchain Your Web Development With Django

Details, continued... <td>Steps</td> <dd> <ol> {% for step in drink.steps.all %} <li>{{ step.text }}</li> {% endfor %} </ol> </dd> </dl>{% endblock %}

Page 27: Unchain Your Web Development With Django

Just one more piece: urls

urls.py:

from cocktails.recipes.views import DrinkDetailView, DrinkListView

urlpatterns = [ ... url(r'^drinks/$', DrinkListView.as_view(), name='drink_list'), url(r'^drinks/(?P<pk>[0-9]+)/$', DrinkDetailView.as_view(), name='drink_detail'),]

Page 28: Unchain Your Web Development With Django

The (ugly, ugly) fruits of our labor

Page 29: Unchain Your Web Development With Django

The (slightly less ugly) details

Page 30: Unchain Your Web Development With Django

The full code, plus a bit more

django-cocktails repository: https://github.com/tarkatronic/django-cocktails

Page 31: Unchain Your Web Development With Django

What if I need help?

“I came for the auto generated admin, but I stayed for the community.”-Ola Sitarska

● StackOverflow - http://stackoverflow.com/questions/tagged/django

● django-users mailing list - https://groups.google.com/forum/#!forum/django-users

● IRC: #django on Freenode (I'm often on as TheJoey)

Page 32: Unchain Your Web Development With Django

Now, go learn more!

Django Project website: http://www.djangoproject.com/

Django Girls tutorial: http://tutorial.djangogirls.org/

Getting Started with Django: http://gettingstartedwithdjango.com/

Two Scoops of Django: http://twoscoopspress.org/

Page 33: Unchain Your Web Development With Django

Cheers!