Login

Callable Class View

Author:
zbyte64
Posted:
May 17, 2008
Language:
Python
Version:
.96
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

mike_45 (on February 3, 2009):

Nice. Just what the doctor ordered.

#

Please login first before commenting.