On 2011-07-12, at 07:04 , Venkatraman S wrote:
> Comments? (note that i am not passing request object to individual doPOST or
> doGET).
You can trivially do that by using callable objects as views, with a common
superclass for dispatching.
class MethodView(object):
def __call__(self, request, *args, **kwargs):
try:
return getattr(self, request.method)(request, *args, **kwargs)
except AttributeError:
# status 405: method not allowed
GET requests will call the GET method, POST request the POST method, etc…
Note that this behavior is implemented by Django's own
`django.views.generic.base.View` in 1.3 (although it lowercases the method
names before calling them, to better fit Python standards), so you can inherit
from that class and get the same result without writing your own superclass. It
also provides a default 405 METHOD NOT ALLOWED implementation which can be
overridden.
A small difference is that request, args and kwargs are set as instance members
of the view, they're not passed along.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.