如何在应用程序引擎cron作业中访问应用程序的路线?
问题描述:
所以,我有我的应用程序定义的内部main.py
的路线,是这样的:如何在应用程序引擎cron作业中访问应用程序的路线?
app = webapp2.WSGIApplication([
webapp2.Route('/', handler=HomePage, name="home")
])
里面的cron作业,我似乎无法访问应用程序的途径,例如这不起作用:
self.uri_for('home')
我在什么地方找到的在线修复它的摘要,但它是丑陋的使用:
cls.app.router.add(r)
凡r
将是一系列路线。
有没有一种方法可以访问应用程序引擎cron作业中的应用程序路线?
答
你的例子是不正确的,它似乎是simple routes和extended routes之间的交叉。
为了能够使用self.uri_for('home')
你需要使用命名路由,即延长路线:
app = webapp2.WSGIApplication([
webapp2.Route(r'/', handler=HomePage, name='home'),
])
有了到位self.uri_for('home')
应该工作,假设self
是webapp2.RequestHandler
实例。
解决方法只是看起来丑陋,但就是差不多就是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)