from django.http import HttpResponse as response
from django.http import HttpResponseNotAllowed
class ViewClass:
def __call__(self, request, *args, **kwargs):
self.request = request
methods = ['POST', 'GET']
self.methods = [method for method in dir(self)\
if callable(getattr(self, method)) and method in methods]
if request.method in self.methods:
view = getattr(self, request.method)
return view(*args, **kwargs)
else:
return HttpResponseNotAllowed(self.methods)
class IndexView(ViewClass):
def GET(self):
return response("all ok %s" % self.request.method)
def POST(self):
return response("all ok %s" % self.request.method)
index = IndexView()
Comments
In urls.py the view is "index"
#
That's not Thread safe..
If two views are called in the same time.. you'll be exposed to a huge problem :
Having self.request in the first call context matching to the request of the second one.
If you want something great, refer to the new generic class-based views. http://docs.djangoproject.com/en/dev/topics/class-based-views/
#