Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5937171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:30:05+00:00 2026-05-22T15:30:05+00:00

With function based Django view it was simple to switch between several different views

  • 0

With function based Django view it was simple to switch between several different views based on a condition, e.g. something like:

def base_view(request):
    if some_condition():
        return foo_view(request)
    else:
        return bar_view(request)

I can’t find a simple way to do the same with the new class-based generic views. The only way I can think of is to redisrect, which I would like to avoid for various reasons:

def base_view(request):
    if some_condition():
        return redirect(reverse("name_of_url_to_class-based_view_foo"))
    else:
        return redirect("/url_to_class-based_view_bar/")

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:30:06+00:00Added an answer on May 22, 2026 at 3:30 pm

    This is equivalent to your example with class based views.

    class FooView(View):
        pass
    
    class BarView(View):
        pass
    
    class BaseView(View):
        # staticmethod to avoid adding 'self' to the arguments
        foo_view = staticmethod(FooView.as_view())
        bar_view = staticmethod(BarView.as_view())
    
        def dispatch(self, request, *args, **kwargs):
            if some_condition():
                return self.foo_view(request, *args, **kwargs)
            else:
                return self.bar_view(request, *args, **kwargs)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function in Python that returns different fixed values based
What's the correct way to extend django's class based views to display a page
If I had a function like this: function validateSomething() { var valid; $.post(something.php, {x:y},
So I'm trying to get TinyMCE up and running on a simple view function,
I'd like to build a function in Django that iterates over a set of
Is there some inbuilt function to return weekdayname based on value 1-7? /M
I have a function TRANSLATE which reads value from the xml file based on
How can I get a user-defined function to re-evaluate itself based on changed data
Need to call a filter function on some options based on a radio selected
I'm using MySQL API's function mysql_real_escape_string() Based on the documentation, it escapes the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.