Same function used for GET and POST
|Reported by:||aaronsw||Owned by:||adrian|
|Component:||Database layer (models, ORM)||Version:|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Django encourages the use of the same function for both GET and POST (not to mention DELETE) methods. For example, 'module.method' resolves to:
def method(request): ...
In the web app framework I wrote, it resolves to:
class method: def GET(self, request): ... def POST(self, request): ... ...
This ensures that POST functions don't accidentally leak into GETs. If both POST and GET want to reuse code, then they can add a new method to the class. This also allows this style:
... (r'^/method/(.*)', 'module.method') ... class method: GET = web.autodelegate("GET_") POST = web.autodelegate("POST_") def GET_(self, request): ... # index def GET_pagename(self, request): ... def POST_pagename(self, request): ...
which makes adding new URLs easy. The code for autodelegate is simply:
def autodelegate(prefix=''): def internal(self, request, arg): func = prefix+arg if hasattr(self, func): return getattr(self, func)(request) else: raise NotFound return internal
Change History (6)
comment:5 Changed 10 years ago by adrian
- Resolution set to worksforme
- Status changed from new to closed