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. Hi, I’m Markus Holtermann • Django Core Developer • Worked

    at ubuntuusers.de @m_holtermann • github.com/MarkusH
  2. • Web framework • Ridiculously fast • Fully loaded •

    Reassuringly secure • Exceedingly scalable • Incredibly versatile
  3. Architecture • Model ० Database Abstraction ० Object Relational Mapper

    (ORM) • Template ० Design ० HTML • View ० Business Logic
  4. • Security • Backwards compatibility • Rapid Prototyping • Loads

    of features • “Djangoverse” • Community • Loads of features • “Too much magic” Strengths – Weaknesses
  5. 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})
  6. 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)
  7. 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)
  8. 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
  9. 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)
  10. 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
  11. 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
  12. 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<pk>\d+)/$', TodoItemDetailView.as_view(), name='detail'), url(r'^(?P<pk>\d+)/update/$', TodoItemUpdateView.as_view(), name='update'), url(r'^(?P<pk>\d+)/delete/$', TodoItemDeleteView.as_view(), name='delete'), url(r'^$', TodoItemListView.as_view(), name='list'), ]
  13. 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')), ]
  14. • 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
  15. • 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/
  16. • REST API Tools: ० Django Rest Framework: ▪ django-rest-framework.org

    ० django-nap: ▪ django-nap.rtfd.org • Search ० Elasticsearch (larger+ sites) ० Haystack + Whoosh (small sites)