framework Django. Ao terminar o tutorial você tem uma aplicação web tot alment e funcional: um blog. https://tutorial.djangogirls.org/pt/ O que é o tutorial do ?
HTML Livro 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})
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')
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') E nós nem cuidamos de todos os casos!
em métodos separados As classes podem ser estendidas, permitindo reutilização de código Você pode usar vários Mixins pré-prontos para evitar reinventar a roda! Mas… porque usar o CBV?
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})
import BookForm from .models import Book class BookUpdateView(UpdateView): model = Book template_name = 'my-book-edit.html' form_class = BookForm success_url = '/my-books'
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 linhas vs 4 linhas!
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})
diferente? Todas as base class tem um comportamento específico. No caso do DetailView o padrão é buscar pelo ID. Mas e se eu quiser olhar para um atributo diferente?
class BooksView(ListView): model = Book template_name = 'my-books.html' def get_queryset(self): return Book.filter(in_stock=True) Um QuerySet (conjunto de busca) é, em essência, uma lista de objetos de um dado modelo.
import Book class BookDetailView(LoginRequiredMixin, DetailView): model = Book template_name = 'my-book-detail.html' login_url = '/login' Autenticação está a apenas uma linha!
Serializers define the API representation class BookSerializer(serializer.HyperlinkModelSerializer): class Meta: model = Book fields = ['title'] class BookViewSet(viewset.ModelViewSet): queryset = Books.objects.all() serializer_class = BookSerializer Django Rest Framework