python - 使用 GAE 的 Yaml 寻址和模板不起作用? TemplateDoesNotExist 只是通过 yaml 中的简单更改?

标签 python google-app-engine templates django-templates yaml

我在通过 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/

相关文章:

python - 抓取页面时找不到视频网址

python - 如何在没有引用对象实例的情况下获取集合名称?

php - Amazon S3 PHP SDK v3 的 Google 存储授权 header 不正确

python - 导入云存储,ImportError : No module named google. appengine.api

c++ - 在原始类型模板特化之间转换

c++ - 返回类型重载函数? (重访)

c++ - 作为仿函数模板的模板参数

python - 如何在 TensorFlow 中计算张量的加权和?

python - 如何使 C++ 将二维数组返回给 Python

python - GAE组织数据结构问题