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

What's beyond the Django Girls tutorial: an int...

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

Leticia Portella

October 13, 2019
Tweet

More Decks by Leticia Portella

Other Decks in Technology

Transcript

  1. 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?
  2. Django 101 Request: /welcome Response from django.shortcuts import render def

    welcome(request): return render(request, 'welcome.html')
  3. 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!
  4. 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})
  5. 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')
  6. 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!
  7. 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.
  8. Django 201 Request: /welcome Response from django.views.generic import TemplateView class

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

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

    .models import Book class BooksView(ListView): model = Book template_name = 'my-books.html'
  11. 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})
  12. 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. 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!
  14. 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'
  15. 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})
  16. 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?
  17. 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
  18. 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()), ]
  19. 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'