Benefits of class-based views

suppose you have url like this:

/api/<method>

In function based view it's like:

  def myview(req, method=''):
    if method=='callA':
      return ""
    elif method=='callB':
      return ""
    else:
      return ""

The problem is you can't enumerate every callA and callB.

But for class based view it's trivial to write more elegant code like:

  class myview(ViewRouter):
      def callA(req):
          return ''
      def callB(req):
          return ''
      def default(req):
          return ''

Just want to put up an example of what class-based views are useful in Web development. The powerful object introspection allows you to map URL behaviors to a well defined class.

Comments