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?

有帮助吗?

解决方案

I am pretty sure you need to return

View1.as_view() 

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top