两种方法:
# routes: (passed to WSGIApplication)
[..snip..]
('/note/add', AddNoteHandler),
('/note/delete/(.+)', DeleteNoteHandler),
('/note/view/(.+)', ViewNoteHandler),
('/note', ListNotesHandler),
[..snip..]
..对..
('/note/(.*)', NoteHandler)
# which moves all the code from many RequestHandlers to one..
# ..but with a lot of branching inside, e.g.
class NoteHandler(webapp.RequestHandler):
def get(self, params):
params = params.split('/')
action = params[0]
id = params[1]
# start switching by action
def post(self, params):
params.split('/'):
# POST case
在某些object
(在这种情况下,note
)为每个CRUD操作分别设置处理程序将导致在这些处理程序中重复执行更多代码,以及一个庞大的路由列表。另一方面,我觉得这将有一个更清洁,更好的结构,以一种处理程序为所有的方式做到这一点。
对此有何想法?
答案 0 :(得分:2)
让所有共享代码的处理程序子类化为基本处理程序(子类webapp.RequestHander
)。
通过这种方式,您可以正确分离路由和处理程序,并可以分解公共代码。