I need to retrieve the value of the pk written in the url in my class based view:
path('<int:pk>/data/', views.DataView.as_view(), name='data'),
However, what I saw everywhere was the pk retrieved in the definition of methods like get or post.
What I want is to be able to use it to define my class variables, because I need to do get the same value from this pk in every method inside my class, like below:
class DataView(ContextMixin, UpdateView):
pk = get_pk_from_url
...
value = ...
def get(self, request, *args, **kwargs):
value = self.value
# Do something with value
def post(self, request, *args, **kwargs):
value = self.value
# Do something with value
I got an idea while writing this question, which is to define a method that will do what I need, then calling this method in my other methods.
class DataView(ContextMixin, UpdateView):
def get_value(self):
self.pk = self.kwargs['script_id']
...
self.value = ...
def get(self, request, *args, **kwargs):
self.get_value()
value = self.value
# Do something with value
def post(self, request, *args, **kwargs):
self.get_value()
value = self.value
# Do something with value
However, I don't know if there's another way, which is why I still wanna ask my question.