Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django run another class-based view (CBV) in a CBV?

so I have a CBV (A), CBV (B), and a url like

regex=r'^(?P<slug>[-\w]+)/(?P<app>[-\w]+)'

I want to read in the slug and app parameters with (A) and then based on those, redirect it to an appropriate CBV, possible (B). I don't want to redirect the user with HttpResponseRedirect or anything like that, but instead basically run another CBV as if it were the one being called. How do I run another CBV, like (B), directly/internally from a CBV (A)?

like image 614
Derek Avatar asked Dec 03 '25 10:12

Derek


2 Answers

You can call it that way:

class CBViewA(View):
    def dispatch(self, request, *args, **kwargs):
        if kwargs['slug'] = "some slug":
            return CBViewB.as_view()(request, *args, **kwargs)
        else:
            return super(CBViewA, self).dispatch(request, *args, **kwargs)
like image 68
Vladislav Mitov Avatar answered Dec 05 '25 00:12

Vladislav Mitov


You can call it that way:

class CBViewA(View):
    def dispatch(self, request, *args, **kwargs):
        if kwargs['slug'] = "some slug":
            return CBViewB.as_view()(request, *args, **kwargs)
        else:
            return super(CBViewA, self).dispatch(request, *args, **kwargs)
like image 21
Vladislav Mitov Avatar answered Dec 04 '25 23:12

Vladislav Mitov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!