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

What's beyond the Django Girls tutorial: an introduction to class based views

What's beyond the Django Girls tutorial: an introduction to class based views

96f6148ae5af63f90246a3817686b457?s=128

Leticia Portella

October 13, 2019
Tweet

More Decks by Leticia Portella

Other Decks in Technology

Transcript

  1. What's beyond Django Girls tutorial an introduction to Class Based

    Views
  2. LETICIA PORTELLA /in/leportella @leportella @leleportella leportella.com pizzadedados.com

  3. A tutorial to teach people to develop using Django. Once

    you've finished the tutorial, you will have a small working web application: your own blog. https://tutorial.djangogirls.org What’s the tutorial?
  4. Django 101 Request: /welcome Response

  5. Django 101 Request: /welcome Response from django.shortcuts import render def

    welcome(request): return render(request, 'welcome.html')
  6. Django 102 - Listing books Request: /mybooks Response from django.shortcuts

    import render from .models import Book def get_books_list(request): books = Book.objects.all() return render(request, 'my-books.html', {'object_list': books}) Get all books!
  7. Django 102 - Listing books

  8. Django 103 - Get specific book Request: /mybooks/1 Response Book

    1: A game of thrones from django.http import Http404 from django.shortcuts import render from .models import Book def get_book_by_id(request, pk): try: book = Book.objects.get(id=pk) except Book.DoesNotExist: raise Http404("Book does not exist") return render(request, 'my-book-detail.html', {'object': book})
  9. Django 104 - Update book from django.http import Http404 from

    django.shortcuts import redirect, render from .forms import BookForm from .models import Book def edit_book(request, pk): try: book = Book.objects.get(id=pk) except Book.DoesNotExist: raise Http404("Book does not exist") if request.method == 'GET': form = BookForm(instance=book) return render(request, 'my-book-edit.html', {'form': form, 'object': book}) if request.method == 'POST': book = Book.objects.get(id=pk) form = BookForm(request.POST, instance=book) if form.is_valid(): form.save() return redirect('/old/my-books')
  10. Django 104 - Update book from django.http import Http404 from

    django.shortcuts import redirect, render from .forms import BookForm from .models import Book def edit_book(request, pk): try: book = Book.objects.get(id=pk) except Book.DoesNotExist: raise Http404("Book does not exist") if request.method == 'GET': form = BookForm(instance=book) return render(request, 'my-book-edit.html', {'form': form, 'object': book}) if request.method == 'POST': book = Book.objects.get(id=pk) form = BookForm(request.POST, instance=book) if form.is_valid(): form.save() return redirect('/old/my-books') And we didn’t treat all cases!
  11. That feeling…

  12. That feeling…

  13. Class-based views provide an alternative way to implement views as

    Python objects instead of functions. Class Based Views! • Organization of code • Mixins (multiple inheritance) to make use of reusable components.
  14. Django 201 Request: /welcome Response from django.views.generic import TemplateView class

    WelcomeView(TemplateView): template_name = 'welcome.html'
  15. Django 201 Request: /welcome Response from django.views.generic import TemplateView class

    WelcomeView(TemplateView): template_name = 'welcome.html'
  16. Django 202 - Listing books from django.views.generic import ListView from

    .models import Book class BooksView(ListView): model = Book template_name = 'my-books.html'
  17. Django 202 - Listing books from django.views.generic import ListView from

    .models import Book class BooksView(ListView): model = Book template_name = 'my-books.html' from django.shortcuts import render from .models import Book def get_books_list(request): books = Book.objects.all() return render(request, 'my-books.html', {'object_list': books})
  18. Django 203 - Update book from django.views.generic import UpdateView from

    .forms import BookForm from .models import Book class BookUpdateView(UpdateView): model = Book template_name = 'my-book-edit.html' form_class = BookForm success_url = '/my-books'
  19. Django 203 - Update book from django.views.generic import UpdateView from

    .forms import BookForm from .models import Book class BookUpdateView(UpdateView): model = Book template_name = 'my-book-edit.html' form_class = BookForm success_url = '/my-books' 13 lines vs 4 lines!
  20. Django 204 - Get specific book from django.views.generic import DetailView

    from .models import Book class BookDetailByIdView(DetailView): model = Book template_name = 'my-book-detail.html'
  21. Django 204 - Get specific book from django.views.generic import DetailView

    from .models import Book class BookDetailByIdView(DetailView): model = Book template_name = 'my-book-detail.html' from django.http import Http404 from django.shortcuts import render from .models import Book def get_book_by_id(request, pk): try: book = Book.objects.get(id=pk) except Book.DoesNotExist: raise Http404("Book does not exist") return render(request, 'my-book-detail.html', {'object': book})
  22. But what if we want different behaviour? All base classes

    have a specific behaviour. In the DetailView the default is look for id. But what if we want to look for a different code?
  23. But what if we want different behaviour? from django.views.generic import

    DetailView from .models import Book class BookDetailByCodeView(DetailView): model = Book template_name = 'my-book-detail.html' def get_object(self): return Book.objects.get(code=self.kwargs['code']) GET /mybooks/123abc456
  24. But what if we want different behaviour? from django.views.generic import

    DetailView from .models import Book class BookDetailByCodeView(DetailView): model = Book template_name = 'my-book-detail.html' def get_object(self): return Book.objects.get(code=self.kwargs['code']) GET /mybooks/123abc456 # urls.py urlpatterns = [ url(r’^mybooks/(?P<code>[-\w]+)', views.BookDetailByCodeView.as_view()), ]
  25. Now, you may ask… How did you know which method

    to change?
  26. ccbv.co.uk <3

  27. ccbv.co.uk <3

  28. ccbv.co.uk <3

  29. ccbv.co.uk <3

  30. Anything else?

  31. Anything else? Yes! Authentication!

  32. Authentication is only one line away! from django.contrib.auth.mixins import LoginRequiredMixin

    from django.views.generic import DetailView from .models import Book class BookDetailView(LoginRequiredMixin, DetailView): model = Book template_name = 'my-book-detail.html' login_url = '/login'
  33. Wanna see the real code? https://github.com/leportella/class-based-views-example https://leportella.com/english/2017/11/24/classe-based- views-on-django.html or http://bit.ly/classbasedviews

  34. Come work with me @ Stripe we are hiring! https://stripe.com/jobs

  35. LETICIA PORTELLA /in/leportella @leportella @leleportella leportella.com pizzadedados.com