chaonextdoor
chaonextdoor

Reputation: 5139

How to navigate to different class views in django based on user type?

Suppose the url is like this: www.example.com/<username> and the user has two types: regular and premium. I wanna use two different views to generate these two different user home pages. How should I write the corresponding urls.py and views.py files to achieve this. By the way, the two different views have been set up and both are class-based view, I just don't know how to do this switch based on user type.

Upvotes: 0

Views: 489

Answers (1)

jproffitt
jproffitt

Reputation: 6355

You can have a middle view which "dispatches" the correct view. That way, you can still have one url. So just point that one url to this view:

class HomePageDispatchView(View):
    def dispatch(self, request, *args, **kwargs):
        if request.user.type == PREMIUM:
            return PremiumHomePage.as_view()(request, *args, **kwargs)
        else:
            return RegularHomePage.as_view()(request, *args, **kwargs)

Upvotes: 3

Related Questions