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.

  2. Hi, I’m Markus Holtermann • Django Core Developer • Worked

    at ubuntuusers.de @m_holtermann • github.com/MarkusH
  3. What is Django?

  4. • Web framework • Ridiculously fast • Fully loaded •

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

    (ORM) • Template ० Design ० HTML • View ० Business Logic
  6. Webserver View ORM Database Template URLConf Forms Source: http://www.django-introduction.com/index.en.html#/4/2

  7. Strengths – Weaknesses

  8. • Security • Backwards compatibility • Rapid Prototyping • Loads

    of features • “Djangoverse” • Community • Loads of features • “Too much magic” Strengths – Weaknesses
  9. Example ToDo list

  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})
  11. Views • Create • Read • Update • Delete •

    List
  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)
  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)
  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
  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)
  16. class TodoItemUpdateView(UpdateView): # … def get_success_url(self): return self.object.get_absolute_url() Views –

    Update (2)
  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
  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
  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<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'), ]
  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')), ]
  21. Settings LOGIN_REDIRECT_URL = ‘todo:list’

  22. Testing – Testability

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

  24. Support – Get Help

  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
  26. Recommendations

  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/
  28. Tools / 3rd Party Libraries

  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)
  30. Questions