Google app engine 带有宏的编译模板在app engine上不起作用
我使用Jinja2编译模板和模块加载器从数据存储中加载编译模板(python代码)。但当我的模板包含宏时,它在appengine:TypeError上不起作用:“NoneType”对象不可调用 但在appenginesdk中,它运行良好。 当我跳过宏调用时,我会收到相同的错误 如果没有宏,它可以正常工作。在没有解决这个宏问题的方法的情况下,我在模板中调用python函数来实现宏的功能 更新:这是导致错误的模板源代码:Google app engine 带有宏的编译模板在app engine上不起作用,google-app-engine,python-2.7,module,jinja2,gae-module,Google App Engine,Python 2.7,Module,Jinja2,Gae Module,我使用Jinja2编译模板和模块加载器从数据存储中加载编译模板(python代码)。但当我的模板包含宏时,它在appengine:TypeError上不起作用:“NoneType”对象不可调用 但在appenginesdk中,它运行良好。 当我跳过宏调用时,我会收到相同的错误 如果没有宏,它可以正常工作。在没有解决这个宏问题的方法的情况下,我在模板中调用python函数来实现宏的功能 更新:这是导致错误的模板源代码: {% extends "mainpage.html" %} {% block
{% extends "mainpage.html" %}
{% block form %}
{% macro test_macro(name) %}
<p>{{ name }}</p>
{% endmacro %}
<div>
{{ test_macro('John Doe') }}
</div>
{% endblock %}
为了使其工作,我必须添加一个内联导入:
from jinja2.runtime import Macro # import again ?????
l_test_macro = Macro(environment, macro, 'test_macro', ('name',), (), False, False, False)
谁能解释一下,我怎么会失去我的进口货???我只有在应用程序引擎中有这个问题,而不是在SDK中???这是一个名称空间问题吗?我可以通过将模块添加到sys.modules中来解决它。但我不明白为什么在我使用宏时它在SDK中工作而不是在GAE中工作 这是我更改的模块加载器的代码
def get_module(self, environment, template):
# Convert the path to a module name
name = template.replace('.html', '').replace('.txt','').replace('/', '.') # NO extensions
module = None
if self.package == None : # load from db and not from package
logging.info('load module : ' + name) # load module from runtimes db
if name in sys.modules : return sys.modules[name] # already imported
try :
runtime = models.Runtimes.rtimes_get_by_key_name(template)
module_code = db.Text(runtime.compiled)
module = imp.new_module(name)
exec module_code in module.__dict__
sys.modules[name] = module # add to sys modules, so no import again
return module
except (ImportError, AttributeError):
logging.error('load failed : ' + name)
else : .... # load from package
raise TemplateNotFound(template)
from jinja2.runtime import Macro # import again ?????
l_test_macro = Macro(environment, macro, 'test_macro', ('name',), (), False, False, False)
def get_module(self, environment, template):
# Convert the path to a module name
name = template.replace('.html', '').replace('.txt','').replace('/', '.') # NO extensions
module = None
if self.package == None : # load from db and not from package
logging.info('load module : ' + name) # load module from runtimes db
if name in sys.modules : return sys.modules[name] # already imported
try :
runtime = models.Runtimes.rtimes_get_by_key_name(template)
module_code = db.Text(runtime.compiled)
module = imp.new_module(name)
exec module_code in module.__dict__
sys.modules[name] = module # add to sys modules, so no import again
return module
except (ImportError, AttributeError):
logging.error('load failed : ' + name)
else : .... # load from package
raise TemplateNotFound(template)