Why don't Django and CherryPy support HTTP verb-based dispatch natively?

I can't speak for Django, but in CherryPy, you can have one function per HTTP verb with a single config entry: request. Dispatch = cherrypy.dispatch. MethodDispatcher() However, I have seen some situations where that's not desirable One example would be a hard redirect regardless of verb Another case is when the majority of your handlers only handle GET.It's especially annoying in that case to have a thousand page handlers all named 'GET'.

It's prettier to express that in a decorator than in a function name: def allow(*methods): methods = list(methods) if not methods: methods = 'GET', 'HEAD' elif 'GET' in methods and 'HEAD' not in methods: methods. Append('HEAD') def wrap(f): def inner(*args, **kwargs): cherrypy.response. Headers'Allow' = ', '.

Join(methods) if cherrypy.request. Method not in methods: raise cherrypy. HTTPError(405) return f(*args, **kwargs): inner.

Exposed = True return inner return wrap class Root: @allow() def index(self): return "" cowboy_greeting = "Howdy" @allow() def cowboy(self): return self. Cowboy_greeting @allow('PUT') def cowboyup(self, new_greeting=None): self. Cowboy_greeting = new_greeting Another common one I see is looking up data corresponding to the resource in a database, which should happen regardless of verb: def default(self, id, **kwargs): # 404 if no such beast thing = Things.

Get(id=id) if thing is None: raise cherrypy.NotFound() # ...and now switch on method if cherrypy.request. Method == 'GET': CherryPy tries to not make the decision for you, yet makes it easy (a one-liner) if that's what you want.

I can't speak for Django, but in CherryPy, you can have one function per HTTP verb with a single config entry: request. Dispatch = cherrypy.dispatch. MethodDispatcher() However, I have seen some situations where that's not desirable.

One example would be a hard redirect regardless of verb. Another case is when the majority of your handlers only handle GET. It's especially annoying in that case to have a thousand page handlers all named 'GET'.

It's prettier to express that in a decorator than in a function name: def allow(*methods): methods = list(methods) if not methods: methods = 'GET', 'HEAD' elif 'GET' in methods and 'HEAD' not in methods: methods. Append('HEAD') def wrap(f): def inner(*args, **kwargs): cherrypy.response. Headers'Allow' = ', '.

Join(methods) if cherrypy.request. Method not in methods: raise cherrypy. HTTPError(405) return f(*args, **kwargs): inner.

Exposed = True return inner return wrap class Root: @allow() def index(self): return "" cowboy_greeting = "Howdy" @allow() def cowboy(self): return self. Cowboy_greeting @allow('PUT') def cowboyup(self, new_greeting=None): self. Cowboy_greeting = new_greeting Another common one I see is looking up data corresponding to the resource in a database, which should happen regardless of verb: def default(self, id, **kwargs): # 404 if no such beast thing = Things.

Get(id=id) if thing is None: raise cherrypy.NotFound() # ...and now switch on method if cherrypy.request. Method == 'GET': ... CherryPy tries to not make the decision for you, yet makes it easy (a one-liner) if that's what you want.

I believe the decision for django was made because usually just GET and POST is enough, and that keeps the framework simpler for its requirements. It is very convenient to just "not care" about which verb was used. However, there are plenty other frameworks that can do dispatch based on verb.

I like werkzeug, it makes easy to define your own dispatch code, so you can dispatch based on whatever you want, to whatever you want.

3 Even though usually GET and POST are enough, the number of verbs is not the issue here. I just can't think of a single use case where the controller (called "view" in Django) should do exactly the same for both GET and POST. – martin Aug 10 '09 at 14:01 1 GET and POST are very common, I agree.

However, I still don't see why the default should be to completely ignore the verb. – martin Aug 13 '09 at 0:28 @martin: why not? They're about the same thing.

You can easily write an application that doesn't care if the request came as a get or as a post. – nosklo Aug 13 '09 at 1:55 2 @nosklo: because they are not the same thing at all. Cases where the verb could be safely ignored are very strange (i still fail to see a single one).

Therefore, these frameworks make the most uncommon case (verb doesn't matter) the default, and the most common case (verb matters) an optional, non-standardized feature. I think it should be the other way around. – martin Sep 9 '09 at 9:35.

Because this is not hard to DIY. Just have a dictionary of accepted verbs to functions in each class. Def dispatcher(someObject, request): try: return someObject.

AcceptedVerbsrequest.method() except: return http. HttpResponseNotAllowed(someObject.acceptedVerbs.keys()).

This one looks nice. Any info on how to wire that dispatcher into a simple Django app? – martin Aug 10 '09 at 14:05 I have a utilities file that I include on my more specific views files.

Each specific file knows which object to use when calling this dispatcher function. – geowa4 Aug 10 '09 at 14:32 Sorry, I still don't see it clear. How much code (besides your dispatcher function) is necessary to wire an url pattern to a verb-specific handler?

Could you add an example? – martin Aug 10 '09 at 14:53 import my_views_utils. Py followed later by dispatcher(someHandlerObjectOfYours, request) in the handler function that you define in your urls.py.It's not Rails, but it works just as well.

– geowa4 Aug 10 '09 at 15:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions