GAE中的Yaml地址和模板不工作?只因yaml简单更改就出现TemplateDoesNotExist?
我在使用Python的Google App Engine时遇到了一个问题。
具体来说,当我在YAML文件中设置静态文件夹时,我无法访问我的模板文件;而当我把这个设置去掉后,就可以访问了。看看我的文件结构。
this is my file structure
-src\
----\calc.py
----\main.py
----\index.html
----templ\
---------\calc.html
---------\js
---------\css
YAML:
handlers:
- url: /.* script: main.py
MAIN.PY
def main():
application = webapp.WSGIApplication([
('/', MainPage),
('/calc',Calc)
], debug=True)
wsgiref.handlers.CGIHandler().run(application)
Calc.py
class Calc(webapp.RequestHandler):
def get(self):
temp = os.path.join(os.path.dirname(__file__), 'templ/calc.html')
outstr = template.render(temp, temp_val)
self.response.out.write(outstr)
结果是: 状态:200 OK 内容类型:text/html; charset=utf-8 我可以访问我的文件,模板的地址也能正常工作
但是……
当我在YAML文件中添加以下一行来访问我的CSS和JS等文件时,就无法访问了。
YAML:
handlers:
- url: /.*
script: main.py
- url: /templ
static_dir: templ
or If i change order of them :
YAML:
handlers:
- url: /templ
static_dir: templ
- url: /.*
script: main.py
这两者都无法正常工作,出现了错误。
状态:500 内部服务器错误
Traceback (most recent call last): File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/_webapp25.py", line 701, in __call__ handler.get(*groups) File "/Users/em/Documents/workspace/NerkhARZ/src/calc.py", line 26, in get outstr = template.render(temp, temp_val) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/template.py", line 88, in render t = load(template_path, debug) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/template.py", line 185, in load return _load_user_django(path, debug) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/template.py", line 110, in _load_user_django template = django.template.loader.get_template(file_name) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django_0_96/django/template/loader.py", line 79, in get_template source, origin = find_template_source(template_name) File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django_0_96/django/template/loader.py", line 72, in find_template_source raise TemplateDoesNotExist, name TemplateDoesNotExist: calc.html
请帮我解决这个问题,应该有简单的解决办法。我真的不相信Google App Engine会这么糟糕……
提前谢谢你们!
2 个回答
1
除了Wooble提到的问题,你的static_dir
处理器是在.*
脚本处理器之后添加的。处理器是按顺序执行的,所以static_dir
处理器永远不会被执行,因为所有的请求都被.*
处理器捕获了。
3
在 app.yaml
文件中标记为静态的文件,应用程序的代码在 Python 运行时是无法访问的。这些文件只会直接发送给用户的浏览器,前提是用户的请求符合 app.yaml
中的规则。
不要把模板文件标记为静态。只有那些应该原封不动地提供给用户的文件,比如 JavaScript、CSS 和图片,才能标记为静态。