龙卷风宁静处理程序类 [英] Tornado Restful Handler Classes

查看:119
本文介绍了龙卷风宁静处理程序类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读并发现了此答案有关此问题的问题,但我真正想知道的是如何实现此结构以及需要多少个处理程序类:

I've read around and found this answered question about a problem relating to this but what I really want to know is how to implement this structure and how many handler classes I need:

1  GET    /items        #=> index
2  GET    /items/1      #=> show
3  GET    /items/new    #=> new
4  GET    /items/1/edit #=> edit
5  PUT    /items/1      #=> update
6  POST   /items        #=> create
7  DELETE /items/1      #=> destroy

我当时正在考虑将2,5,7映射到路由到/items/[0-9] +的单个处理程序,并为项,items/new和/items/[0-9] +设置3个新处理程序/编辑.不利的一面是,对于单个资源有4个处理程序,这似乎是次佳的解决方案.

I was thinking having 2,5,7 mapped to a single handler routed to /items/[0-9]+ and having 3 new handlers for the items, items/new and /items/[0-9]+/edit. The downside is that it felt like a sub-optimal solution to have 4 handlers for a single resource.

我对正确的路由/处理/Web应用程序非常陌生,但是在开始学习某些东西之前,至少我应该给它一个好书.关于路由多少/更多处理程序,有更好的建议吗?

I'm terribly new to proper routing/handling/webapps but I at least give it a good read before I start on something. Are there any better suggestions for how many/how you route your handlers?

推荐答案

嗯,它在很大程度上是风格上的.在这种情况下,每个请求处理程序 表示从其中一个方法中删除if语句.我认为它 可以更清楚地限制RequestHandlers的数量.最清晰 我认为可以通过一个处理程序和三个路由来实现结果.

Well, it is largely stylistic. Each request handler in this situation represents the removal of an if statement from one of your methods. I think it can be clearer to limit the number of RequestHandlers. The clearest results I think can be achieved with one handler and three routes.

我还丢弃了您的项目3.因为它是项目6的重复项. 拥有一个"items/new"网址确实很重要,然后我们可以将其放回去. 尽管我认为到那时您仍需要另一个处理程序类以保持清晰度.

I've also thrown away your item 3. Because it is a duplication of item 6. If having an 'items/new' url is really important then we could put it back in. Though I think at that point you would need another handler class for clarity.

class ItemHandler(tornado.web.RequestHandler):

    def get(self, item_id=None, edit=False):
        if item_id:
            # get item from db
            if edit:
                new_data_from_query_string = self.get_argument('item_data')
                # do edit, save item
            # return item
        else:
            # return index

    def put(self, item_id):
        data = self.get_argument('item_data')
        # do your update for item

    def post(self):
        data = self.get_argument('item_data')
        # do your item creation

    def delete(self, item_id):
        # do your deletion for item_id

然后可以像这样创建实际的应用程序:

Then the actual application could be created like this:

tornado.web.application([
    (r'/items$', ItemHandler),
    (r'/items/(\d+$)', ItemHandler),
    (r'/items/(\d+)/(edit)$', ItemHandler),
])

如果您想要'/items/new'网址,那么我可能建议您将其放在 一个单独的处理程序,因为否则会使逻辑过于复杂.

If you want the '/items/new' url then I would probably suggest putting that in a separate handler because it would otherwise make the logic overly complex.

这篇关于龙卷风宁静处理程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆