Question

I'd like to use two different CBVs under one url - depending on a user's permissions. The views are quite different, and I do not want to blend the two views into just one.

What would be a good approach for this? I am thinking of something like this:

class View1(TemplateView):
   ...

class View2(TemplateView):
   ...

def unified_view(request):
    if request.user.has_perm('...'):  # here I could decide which CBV to use
        return View1
    return View2

unified_view would be used in the app's urls.py and decides which CBV to use. Obviously return View1 does not work. What would be the correct call?

Was it helpful?

Solution

I am pretty sure you need to return

View1.as_view() 

OTHER TIPS

I would not recommand you to do this, class based view should be simple and do one thing.

If you have to do this, you can override get_context_data or get_template_names of TemplateView for different users.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top