On , I learnt ...
About Django’s setup
method for generic view classes
Django 2.2 (released in April 2019)
introduced a setup
method
to the django.views.View
class, which is available to all generic views.
This is intended to be overridden to assign instance attributes that other methods will need.
For example, you would use this to load model instances identified in the URL arguments:
from django.views import generic
from django import shortcuts
from myapp import models
class Frob(generic.TemplateView):
def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)
self.frob = shortcuts.get_object_or_404(models.Frob, pk=kwargs["pk"])
To date, I’ve been overriding dispatch
to do this - didn’t realise there was a
dedicated method.
More in the Django view docs