【问题标题】:How do I do a Rails style before_filter with Google App Engine?如何使用 Google App Engine 执行 Rails 样式 before_filter?
【发布时间】:2010-07-08 23:45:43
【问题描述】:

该应用程序被设置为一个基本的 WSGI 应用程序。我只是想在 requestHandler 运行之前调用一个函数。

我想要一些非常类似于 before_filter 在 Rails 中的工作方式的东西。

谢谢。

【问题讨论】:

    标签: python google-app-engine wsgi django-middleware


    【解决方案1】:

    我会使用装饰器,它与 rails 中的 before_filter 不完全相同,但对你来说可能已经足够了:

    def before_filter(fn):
        def inner_function(self):
            # do stuff before
            return fn(self)
        return inner_function
    
    class MainPage(webapp.RequestHandler):
    
        @before_filter
        def get(self):
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.out.write('Hello, webapp World!')
    

    【讨论】:

      【解决方案2】:

      您可以将“之前”安装为 WSGI 中间件——App Engine 使用 WSGI,就像现在 Python 中的几乎所有 Web 框架和服务器一样。这是an example——它在处理程序运行之后做一些事情,但在之前做这些事情更容易......无论如何,你的中间件“包装”了WSGI应用程序是实际应用程序;-),因此您当然可以在之前、之后或替代之前执行操作;-)。

      有关 WSGI 的更多信息,请参阅here

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 2018-12-25
        • 1970-01-01
        • 2013-09-26
        • 1970-01-01
        • 2021-08-26
        • 1970-01-01
        • 2012-06-11
        • 2010-11-20
        相关资源
        最近更新 更多