Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@meshy
Created March 2, 2012 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meshy/1957251 to your computer and use it in GitHub Desktop.
Save meshy/1957251 to your computer and use it in GitHub Desktop.
Suggested new dispatch flow for class based views.
# @meshy's suggestion:
def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
self.prepare_view(request, handler, *args, **kwargs)
return handler(request, *args, **kwargs)
# Could probably do with a better name...
def prepare_view(self, request, handler, *args, **kwargs):
"""Set local variables before the dispatch handler is called."""
self.request = request
self.args = args
self.kwargs = kwargs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment