Introduction to Django

Introduction to Django

Bd9b4485bcf325b989b5721bd0ef4d91?s=128

Bruno Renié

April 03, 2012
Tweet

Transcript

  1. Django Webmardi - 03.04.2012 @brutasse

  2. $ whoami

  3. “Django is a high-level Python Web framework that encourages rapid

    development and clean, pragmatic design”
  4. None
  5. None
  6. Théorie

  7. Real-world app: Cheese catalog Like / dislike cheeses Twitter authentication

  8. $ pip install Django http://www.pip-installer.org

  9. $ django-admin.py startproject webmardi webmardi/ ├── manage.py └── webmardi ├──

    __init__.py ├── settings.py ├── urls.py └── wsgi.py
  10. manage.py Project toolbox

  11. $ python manage.py startpapp cheese cheese/ ├── __init__.py ├── models.py

    ├── tests.py └── views.py
  12. Models ORM

  13. from django.db import models from ..users.models import User class Cheese(models.Model):

    name = models.CharField(max_length=255) image = models.ImageField(upload_to='cheese') description = models.TextField() class Taste(models.Model): cheese = models.ForeignKey(Cheese, related_name='tastes') user = models.ForeignKey(User) like = models.BooleanField(default=True) class Meta: unique_together = ('cheese', 'user')
  14. Admin Customizable edition interface

  15. from django.contrib import admin from .models import Cheese, Taste class

    CheeseAdmin(admin.ModelAdmin): list_display = ('name', 'image') class TasteAdmin(admin.ModelAdmin): list_display = ('cheese', 'user', 'like') admin.site.register(Cheese, CheeseAdmin) admin.site.register(Taste, TasteAdmin)
  16. Views Request handling

  17. from django.template.response import TemplateResponse from .models import Cheese, Taste def

    cheese_list(request): context = { 'cheeses': Cheese.objects.all(), } return TemplateResponse(request, 'cheese_list.html', context)
  18. URLs HTTP routing

  19. from django.conf.urls import patterns, url from . import views urlpatterns

    = patterns('', url(r'^$', views.cheese_list, name='cheese_list'), url(r'^cheese/(?P<pk>\d+)/$', views.cheese_detail, name='cheese_detail'), url(r'^cheese/(?P<pk>\d+)/like/$', views.like_cheese, name='like_cheese'), url(r'^cheese/(?P<pk>\d+)/dislike/$', views.dislike_cheese, name='dislike_cheese'), url(r'^cheese/add/$', views.add_cheese, name='add_cheese'), )
  20. Templates

  21. <!-- base.html --> <html> <head> <title>{% block title %}{% endblock

    %}</title> </head> <body> {% block content %}{% endblock %} </body> </html>
  22. <!-- cheese_list.html --> {% extends "base.html" %} {% load thumbnail

    markup %} {% block title %}Cheese types{% endblock %} {% block content %} {% for cheese in cheeses %} <h2>{{ cheese.name }}</h2> <img src="{% thumbnail cheese.image 300x300 crop %}"> {{ cheese.description|markdown }} {% endfor %} {% endblock %}
  23. Tests Untested code is by definition broken

  24. from django.core.urlresolvers import reverse from django.test import TestCase class CheeseTest(TestCase):

    def test_home(self): url = reverse('cheese_list') response = self.client.get(url) self.assertContains(response, 'Cheese')
  25. Forms Input validation / sanitization <form> rendering

  26. from django import forms from .models import Cheese class CheeseForm(forms.ModelForm):

    class Meta: model = Cheese
  27. GIS Cryptographic signing Browser testing i18n Flash messages Atom/RSS Email

    Cache Storage Logging Unicode Comments
  28. Search Error reporting HTML5 forms Database migrations CMS REST API

    Background tasks Debugging There's an app for that
  29. Questions‽ Thanks @liip Code: https://github.com/brutasse/webmardi Slides: http://speakerdeck.com/u/brutasse