[Django]-Get url kwargs in class based views

5👍

Thanks to @WillemVanOnsem’s comment, I was able to use the setup method (doc here) in order to define everything I needed to define.
So in the end I get something like that :

class DataView(View):

    def setup(self, request, *args, **kwargs):
        self.pk = kwargs['pk']
        ...
        self.value = ...
        return super().setup(request, *args, **kwargs)


    def get(self, request, *args, **kwargs):
        value = self.value
        

    def post(self, request, *args, **kwargs):
        value = self.value

The setup method being called before the get and post ones, the class attributes declared in it can then be used in the other methods.

Leave a comment