I’m currently experimenting with class based views, because I see options to simplify my code a lot. But I’m somewhat confused by the lifecycle of class base views. Lets start with a simple question:
Let say I have a class base view like
class MyView(View):
# ...
In most examples it is used like this:
urlpatterns = [
path("some_path/", MyView.as_view(), name="some_name"),
]
Assuming my view is able to handle multiple paths. Is there some reason that forbids or discourages to do this:
my_view = MyView.as_view()
urlpatterns = [
path("some_path/", my_view, name="some_name"),
path("other_path/", my_view, name="other_name"),
]
Is it ok to reuse the result of MyView.as_view()
or should I call it once per path
? The background of the question is that MyView
needs to initialize itself. It needs to prepare some internal data structures at startup that will be static during runtime. I would prefer to do it just once. That’s the reason why I would like to reuse the object for multiple paths.
I expect as_view()
to create an instance of MyView
so I would do the initialization in __init__
. But there is also setup
which sounds like it should be use for such tasks. The docs say:
When the view is called during the request/response cycle, the
setup()
method assigns theHttpRequest
to the view’srequest
attribute, and any positional and/or keyword arguments captured from the URL pattern to theargs
andkwargs
attributes, respectively. Thendispatch()
is called.
This suggests that setup
and dispatch
are always called as a pair one after another. They both seem to have the same signature and from the docs I’m not able to understand why there are two methods. Now I have the feeling that I completely misunderstood something, which leaves me confused.
Can somebody tell me where I got lost and where to run one-time initialization code of my view?