我正在尝试在主GAE应用程序中或多或少地支持一个独立的应用程序,该应用程序托管在不同的子域中。
我将此子域的处理程序放在“ oficina”文件夹内的文件“ mediciones.py”中。
所以我有:
class Router(webapp2.RequestHandler):
def get(self):
[... code ...]
class Listado(webapp.RequestHandler):
def get(self):
[... code ...]
等等对于所有必要的处理程序。
在“ main.py内部:
application = webapp2.WSGIApplication([
DomainRoute('ventas.domain.com',
[webapp2.Route(r'/nueva', handler='oficina.mediciones.MedicionNueva', name="nueva-medicion"),
webapp2.Route(r'/listado', handler="oficina.mediciones.Listado", name="listado-mediciones"),
webapp2.Route(r'/medicion/(\d+)/', handler="oficina.mediciones.MedicionDetalles", name="detalles-mediciones"),
webapp2.Route(r'/rellenar_medicion/(\d+)/', handler="oficina.mediciones.MedicionRellenar", name="rellenar-medicion"),
webapp2.Route(r'/editar_medicion/(\d+)/', handler="oficina.mediciones.MedicionEditar", name="editar-medicion"),
webapp2.Route('/', handler="oficina.mediciones.Router")
]),
('/(.+)',
DirectView),
('/?',
HomeView),
], debug=True)
但是当我尝试去ventas.domain.com或ventas.domain.com/listado时,我得到了:
Traceback (most recent call last):
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1272, in default_dispatcher
self.handlers[handler] = handler = import_string(handler)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1851, in import_string
return getattr(__import__(module, None, None, [obj]), obj)
ImportStringError: import_string() failed for 'oficina.mediciones.Router'. Possible reasons are:
- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;
Original exception:
AttributeError: 'module' object has no attribute 'Router'
Debugged import:
- 'oficina' found in '/base/data/home/apps/s~pavimentos-arquiservi-web-hrd/11-0-0.361841284178483516/oficina/__init__.pyc'.
- 'oficina.mediciones' found in '/base/data/home/apps/s~pavimentos-arquiservi-web-hrd/11-0-0.361841284178483516/oficina/mediciones/__init__.pyc'.
- 'oficina.mediciones.Router' not found.
(用“ Listado”替换“ Router”,或为每种情况选择适当的处理程序。
已定义处理程序,但是为什么找不到它呢?
如果应用程序确实是自包含的,则可以考虑使用其他方法将处理程序拆分到单独的模块中。在app.yaml
中,您可以定义其他url前缀,并由其他模块处理。
例如
- url: /oficina/.*
script: ofinina.mediciones.py
- url: /.*
script: main.py
很遗憾,您不能仅通过域名将这些路由分开,因此,根据您的要求,这可能不是您的选择。
作为替代方案,您可以将所需的包导入到main.py中并直接使用类名,而不是为处理程序使用字符串名。这减少了包装的自包含性。但是再说一遍,通过字符串文字引用类名也是如此。