【问题标题】:How to access app's routes inside app engine cron job?如何在应用引擎 cron 作业中访问应用的路由?
【发布时间】:2017-09-15 15:43:01
【问题描述】:

所以我在main.py 中为我的应用定义了路由,类似于:

app = webapp2.WSGIApplication([
    webapp2.Route('/',          handler=HomePage,       name="home")
])

在 cron 作业中,我似乎无法访问应用程序的路由,例如这不起作用:

self.uri_for('home')

我在网上某处找到了修复它的sn-p,但使用起来很丑:

cls.app.router.add(r)

r 是一个路由数组。

有没有办法在应用引擎 cron 作业中访问应用的路由?

【问题讨论】:

    标签: google-app-engine cron google-app-engine-python


    【解决方案1】:

    您的示例不正确,它似乎是simple routesextended routes 之间的交叉。

    为了能够使用self.uri_for('home'),您需要使用命名路由,即扩展路由:

    app = webapp2.WSGIApplication([
        webapp2.Route(r'/', handler=HomePage, name='home'),
    ])
    

    假设selfwebapp2.RequestHandler 实例,self.uri_for('home') 应该工作。

    解决方法看起来很丑陋,但这几乎就是 uri_for 所做的 under the hood

    def uri_for(self, _name, *args, **kwargs):
        """Returns a URI for a named :class:`Route`.
    
        .. seealso:: :meth:`Router.build`.
        """
        return self.app.router.build(self.request, _name, args, kwargs)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2012-06-12
      • 2014-08-18
      • 2014-12-07
      • 2015-01-16
      • 2011-07-10
      相关资源
      最近更新 更多