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

Introduction to Django

Introduction to Django

I gave this talk on May 11, 2015 at the Technical University of Berlin to a Bachelor's degree computer science course.

Markus H

May 11, 2015
Tweet

More Decks by Markus H

Other Decks in Programming

Transcript

  1. Introduction to
    Django
    The web framework for
    perfectionists with deadlines.

    View full-size slide

  2. Hi, I’m Markus Holtermann
    • Django Core Developer
    • Worked at ubuntuusers.de
    @m_holtermann • github.com/MarkusH

    View full-size slide

  3. What is Django?

    View full-size slide

  4. • Web framework
    • Ridiculously fast
    • Fully loaded
    • Reassuringly secure
    • Exceedingly scalable
    • Incredibly versatile

    View full-size slide

  5. Architecture
    • Model
    ० Database Abstraction
    ० Object Relational Mapper (ORM)
    • Template
    ० Design
    ० HTML
    • View
    ० Business Logic

    View full-size slide

  6. Webserver
    View
    ORM
    Database
    Template
    URLConf
    Forms
    Source: http://www.django-introduction.com/index.en.html#/4/2

    View full-size slide

  7. Strengths –
    Weaknesses

    View full-size slide

  8. • Security
    • Backwards
    compatibility
    • Rapid Prototyping
    • Loads of features
    • “Djangoverse”
    • Community
    • Loads of features
    • “Too much magic”
    Strengths – Weaknesses

    View full-size slide

  9. Example
    ToDo list

    View full-size slide

  10. Model
    from django.core.urlresolvers import reverse
    from django.db import models
    from django.utils import timezone
    class TodoItem(models.Model):
    value = models.CharField(max_length=255)
    created = models.DateTimeField(default=timezone.now)
    progress = models.SmallIntegerField(default=0)
    owner = models.ForeignKey(settings.AUTH_USER_MODEL)
    def get_absolute_url(self):
    return reverse('todo:detail', kwargs={'pk': self.pk})

    View full-size slide

  11. Views
    • Create
    • Read
    • Update
    • Delete
    • List

    View full-size slide

  12. from django.contrib.auth.decorators import login_required
    from django.utils.decorators import method_decorator
    from django.views.generic import CreateView
    from .models import TodoItem
    class TodoItemCreateView(CreateView):
    fields = ('value', 'progress',)
    model = TodoItem
    @method_decorator(login_required)
    def dispatch(self, request, *args,**kwargs):
    return super().dispatch(request, *args,**kwargs)
    Views – Create (1)

    View full-size slide

  13. class TodoItemCreateView(CreateView):
    # …
    def form_valid(self, form):
    form.instance.owner = self.request.user
    return super().form_valid(form)
    def get_success_url(self):
    return self.object.get_absolute_url()
    Views – Create (2)

    View full-size slide

  14. from django.views.generic import DetailView
    class TodoItemDetailView(DetailView):
    model = TodoItem
    @method_decorator(login_required)
    def dispatch(self, request, *args,**kwargs):
    return super().dispatch(request, *args,**kwargs)
    def get_queryset(self):
    queryset = super().get_queryset()
    return queryset.filter(owner=self.request.user)
    Views – Read

    View full-size slide

  15. from django.views.generic import UpdateView
    class TodoItemUpdateView(UpdateView):
    fields = ('value', 'progress',)
    model = TodoItem
    @method_decorator(login_required)
    def dispatch(self, request, *args,**kwargs):
    return super().dispatch(request, *args,**kwargs)
    def get_queryset(self):
    queryset = super().get_queryset()
    return queryset.filter(owner=self.request.user)
    Views – Update (1)

    View full-size slide

  16. class TodoItemUpdateView(UpdateView):
    # …
    def get_success_url(self):
    return self.object.get_absolute_url()
    Views – Update (2)

    View full-size slide

  17. from django.core.urlresolvers import reverse_lazy
    from django.views.generic import DeleteView
    class TodoItemDeleteView(DeleteView):
    model = TodoItem
    success_url = reverse_lazy('todo:list')
    @method_decorator(login_required)
    def dispatch(self, request, *args,**kwargs):
    return super().dispatch(request, *args,**kwargs)
    def get_queryset(self):
    queryset = super().get_queryset()
    return queryset.filter(owner=self.request.user)
    Views – Delete

    View full-size slide

  18. from django.views.generic import ListView
    class TodoItemListView(ListView):
    model = TodoItem
    @method_decorator(login_required)
    def dispatch(self, request, *args,**kwargs):
    return super().dispatch(request, *args,**kwargs)
    def get_queryset(self):
    queryset = super().get_queryset()
    return queryset.filter(owner=self.request.user)
    Views – List

    View full-size slide

  19. App URLs
    from django.conf.urls import url
    from .views import (TodoItemCreateView,TodoItemDeleteView,
    TodoItemDetailView, TodoItemListView,TodoItemUpdateView)
    urlpatterns = [
    url(r'^add/$', TodoItemCreateView.as_view(),
    name='create'),
    url(r'^(?P\d+)/$', TodoItemDetailView.as_view(),
    name='detail'),
    url(r'^(?P\d+)/update/$',
    TodoItemUpdateView.as_view(), name='update'),
    url(r'^(?P\d+)/delete/$',
    TodoItemDeleteView.as_view(), name='delete'),
    url(r'^$', TodoItemListView.as_view(), name='list'),
    ]

    View full-size slide

  20. Project URLs
    from django.conf.urls import include, url
    from django.contrib import admin
    urlpatterns = [
    url(r'^todo/', include('todo.urls', namespace='todo')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/', include('django.contrib.auth.urls')),
    ]

    View full-size slide

  21. Settings
    LOGIN_REDIRECT_URL = ‘todo:list’

    View full-size slide

  22. Testing – Testability

    View full-size slide

  23. Code
    Documentation
    It’s just Python.
    Use http://sphinx-doc.org/

    View full-size slide

  24. Support –
    Get Help

    View full-size slide

  25. • Documentation: docs.djangoproject.com
    • #django on irc.freenode.net
    • Mailing list: [email protected]
    • Tutorials
    ० Official: See the documentation
    ० DjangoGirls: tutorial.djangogirls.org
    ० Django Book: Do not use that!
    • Don't be afraid to commit:
    dont-be-afraid-to-commit.rtfd.org

    View full-size slide

  26. Recommendations

    View full-size slide

  27. • Django 1.8.1 (latest stable)
    • Python 3.4+
    • PostgreSQL for production
    • SQLite3 only for development, if at all
    ० Run tests on same database as production
    • Start with a custom user model:
    ० https://docs.djangoproject.com/en/1.8/topics/
    auth/customizing/

    View full-size slide

  28. Tools /
    3rd Party Libraries

    View full-size slide

  29. • REST API Tools:
    ० Django Rest Framework:
    ▪ django-rest-framework.org
    ० django-nap:
    ▪ django-nap.rtfd.org
    • Search
    ० Elasticsearch (larger+ sites)
    ० Haystack + Whoosh (small sites)

    View full-size slide