#24660: In gCBVs, decouple get_context_data() and get_template_names() from get() and post() --------------------------------------+---------------------- Reporter: akulakov | Owner: akulakov Type: Cleanup/optimization | Status: new Component: Generic views | Version: master Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 1 Easy pickings: 0 | UI/UX: 0 --------------------------------------+---------------------- Currently there are a few gCBVs where get_context_data and get_template_names methods are closely coupled with get() and post() methods: if self.object or self.object_list are not created in get() or post(), get_context_data and get_template_names will fail.
The reason for that is probably because respective attrs were meant to be set always in get()/post() to make them available for use in all subsequent methods. However, this creates unnecessary need to override methods in order to set these attrs when customizing or mixing gCBVs. It would be tempting to simply init object=None and object_list=None at class level but unfortunately object=None has the special meaning in CreateView - "object is to be created on form submission and so should not be added to context when form is rendered". I propose the following behaviour: * get_context_data() should not depend on get() and set() and should get the object or list from respective methods * get_template_names() should not error out if the object or object_list are unset. Here are some examples of customized gCBVs where this change leads to a simpler design: {{{ # ContextCreateView is implementation of CreateView according to # https://github.com/django/django/pull/4512 # note that no methods need to be overridden class BooksCreateView(ContextCreateView, ListView): model = Book paginate_by = 10 fields = ["name", "author"] success_url = reverse_lazy("list-books-create") template_name = "books.html" class AuthorDetail2(DetailView, ContextFormView): model = Author form_class = AuthorInterestForm template_name = "author2.html" def form_valid(self, form): # Here, we would have some logic based on user's form inputs return redirect('author2', pk=self.get_object().pk) }}} -- Ticket URL: <https://code.djangoproject.com/ticket/24660> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/051.86c30e29e67af80ea2fcac7674238042%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.