我在通过 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 正常 内容类型:文本/html;字符集=utf-8 我可以访问我的文件并且模板寻址正常工作
BUTTTTTTT
当我将以下行添加到我的 YAML 以访问我的 css 和 js 等时。 IT 无法访问
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
请帮我解决这个问题,一定有简单的解决方案。我真的不相信GAE是那么傻....
先谢谢你
最佳答案
在 app.yaml
中指定为静态的文件不可用于 python 运行时中的应用程序代码。它们仅在响应与 app.yaml
中的正则表达式匹配的请求时直接提供给用户的浏览器。
不要将模板标记为静态。只有应按原样提供给用户的文件(例如 javascript、CSS 和图像)才应标记为静态。
关于python - 使用 GAE 的 Yaml 寻址和模板不起作用? TemplateDoesNotExist 只是通过 yaml 中的简单更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8112722/