Login

Callable Class View

Author:
zbyte64
Posted:
May 17, 2008
Language:
Python
Version:
.96
Tags:
view callable response
Score:
5 (after 5 ratings)

Instead of using a function for your views, this allows you to use a class. For your urls definition it works just as it normally does.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class View(object):
    def __new__(cls, request, **kwargs):
        obj = super(View, cls).__new__(cls)
        return obj(request, **kwargs)
    
    def __init__(self):
        pass
        
    def __call__(self, request, **kwargs):
        pass

class GetPostView(View):
    def __call__(self, request, **kwargs):
        if request.POST:
            return self.Post(request, **kwargs)
        return self.Get(request, **kwargs)
    
    def Get(self, request, **kwargs):
        pass
        
    def Post(self, request, **kwargs):
        pass

More like this

  1. Callable object as signal receiver by diverman 6 years, 3 months ago
  2. Ajax API class by kcarnold 7 years ago
  3. Automatic Folder FileField by depaolim 1 year, 1 month ago
  4. Django BasicAuthMixin by icrni 1 year ago
  5. Language aware cache decorator by bartTC 6 years, 2 months ago

Comments

mike_45 (on February 3, 2009):

Nice. Just what the doctor ordered.

#

Please login first before commenting.