Class-Based Views
The class view is the generic: the ListView (the QuerySet, the template, the pagination), the DetailView (the pk, the no the 404), the CreateView (the form, the POST, the redirect), the UpdateView (the form, the no the redirect), and the DeleteView (the confirm, the no the data). The 5 is the no the code (the model the template the form), and the override is the hook (the get_queryset, the get_context, the no the re-write). The dispatch is the method (the GET, the POST, the no the if), and the form_valid is the save (the no the 2 the save). This chapter covers the 5 (the no the code), the override (the 3 the hook), the dispatch (the method, the no the if), and the form_valid (the save, the no the 2).
The 5 generic
# the 5 GENERIC (the no the code):
from django.views.generic import (
ListView, DetailView,
CreateView, UpdateView,
DeleteView)
from .models import Note
class NoteList(ListView):
model = Note
# the no: the template
# (the auto, the no the
# the 1):
# the notes/note_list.html
context_object_name = "notes"
paginate_by = 20 # the page,
# the no the
# the loop
class NoteDetail(DetailView):
model = Note
# the notes/note_detail.html
class NoteCreate(CreateView):
model = Note
fields = ["title", "body"]
# the the no: the form (the
# the ModelForm, the no the
# the 1), the template,
# the success_url
success_url = "/notes/"
class NoteUpdate(UpdateView):
model = Note
fields = ["title", "body"]
success_url = "/notes/"
class NoteDelete(DeleteView):
model = Note
success_url = "/notes/"
# the confirm.html (the no,
# the no the data, the no
# the the 1)
The 5 generic is the no the code: the ListView is the QuerySet (the model the all, the paginate_by the 20, the context_object_name the notes), the DetailView is the pk (the no the 404, the get_object the auto), the CreateView is the form (the ModelForm the fields, the POST the save, the redirect the success_url), the UpdateView is the form (the no the redirect, the get_object the pk), and the DeleteView is the confirm (the no the data, the confirm.html the no) - the template is the auto (the app/model_list.html, the no the 1), and the no the code (the function the 20 line, the class the 5 line) - the override is the next: the 3 the hook, the no the re-write.
The override and the dispatch
# the OVERRIDE (the 3, the hook, the
# no the re-write):
class NoteList(ListView):
model = Note
def get_queryset(self):
# the no: the base, the
# the filter (the no the
# the all):
qs = super().get_queryset()
return qs.filter(
published=True)
def get_context_data(self,
**kwargs):
ctx = super().get_context_data(
**kwargs)
ctx["q"] = self.request.GET.get(
"q", "")
return ctx
def get_template_names(self):
return ["notes/custom.html"]
# the the 3: the get_queryset (the
# the data), the get_context_data
# (the template's the no), the
# get_template_names (the no)
# the DISPATCH (the method, the no the
# if):
# the: the dispatch's
# the no the 1 (the the no, the
# the no, the no):
class NoteCreate(CreateView):
model = Note
fields = ["title", "body"]
def form_valid(self, form):
# the no: the save, the
# the 1 (the no the 2,
# the no the no):
note = form.save()
# the: the
# the no the 1 (the
# the the no, the no
# the no):
return super().form_valid(
form)
The override is the 3: the get_queryset (the filter the published, the super() the base), the get_context_data (the q the template, the super() the no), and the get_template_names (the custom, the no the auto) - the no the re-write (the super() the call, the no the the the the). The dispatch is the method: the GET the form, the POST the save, and the no the if (the method the no the method, the no the 2 the if) - the form_valid is the save: the form.save() the 1 (the no the 2, the no the no), and the super().form_valid is the redirect (the success_url, the no the again) - the rule is the one: the 5 the no the code, the override the 3 the hook, the dispatch the method, and the form_valid the save - the next chapter's form is the validate the render the error the 1 the class.
The Working Rules
- The 5 generic is the no code, the template the auto. The model the all (the paginate_by the 20), the pk the 404, the fields the ModelForm, and the confirm.html the no data - the app/model_list.html the no the 1, and the function the 20 line (the class the 5 line).
- The override is the 3, the super() the base. The get_queryset the filter (the published), the get_context_data the q, and the get_template_names the custom - the super() the call (the no the re-write), and the no the 3 the 1 (the 1 the view, the no the 3).
- The dispatch is the method, the no the if. The GET the form, the POST the save, and the method the dispatch (the no the 2 the if) - the form_valid the save (the 1, the no the 2), and the super().form_valid the redirect (the success_url, the no the again).
- The mixin is the add, the no the 5 the touch. The LoginRequiredMixin the 403 (the no the login, the redirect the next), and the PermissionRequiredMixin the permission (the no the 403) - the no the 5 the code (the mixin the 1 line, the no the dispatch the re-write).
Summary
The class view is the generic: the 5 (the ListView the QuerySet the paginate, the DetailView the pk the 404, the CreateView the ModelForm the redirect, the UpdateView the form, and the DeleteView the confirm), the override the 3 (the get_queryset the filter, the get_context_data the q, the get_template_names the custom, the super() the no the re-write), the dispatch the method (the GET, the POST, the no the if), and the form_valid the save (the 1, the no the 2, the redirect the success_url). The mixin is the add (the LoginRequiredMixin the 403, the PermissionRequiredMixin the no) - the function the 20 line (the class the 5 line), and the next chapter's form is the validate the render: the Form the 3 (the field, the clean, the 12), the ModelForm the no the code, and the CSRF the no the 403.