Appengine应用程序在部署时无法正常工作 - TemplateNotFound。它适用于localhost

时间:2013-07-09 02:25:32

标签: google-app-engine

我面临与谷歌应用引擎应用程序相同的问题。它适用于SDK(localhost),上传到appengine时无法正常工作。部署成功。我被困在这!!!任何帮助表示赞赏。

以下跟踪日志:

    Traceback (most recent call last):
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~cloud-chess/1.368562751153844474/chessboard.py", line 29, in get
    template = JINJA_ENVIRONMENT.get_template('files/html/chess.html')
  File "/python27_runtime/python27_lib/versions/third_party/jinja2-2.6/jinja2/environment.py", line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/python27_runtime/python27_lib/versions/third_party/jinja2-2.6/jinja2/environment.py", line 693, in _load_template
    template = self.loader.load(self, name, globals)
  File "/python27_runtime/python27_lib/versions/third_party/jinja2-2.6/jinja2/loaders.py", line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/python27_runtime/python27_lib/versions/third_party/jinja2-2.6/jinja2/loaders.py", line 180, in get_source
    raise TemplateNotFound(template)
TemplateNotFound: files/html/chess.html

部署日志:

01:56 PM Host: appengine.google.com
01:56 PM Application: cloud-chess; version: 2
01:56 PM 
Starting update of app: cloud-chess, version: 2
01:56 PM Getting current resource limits.
Password for xxxx.pk@gmail.com: 01:56 PM Scanning files on local disk.
01:56 PM Cloning 17 static files.
01:56 PM Cloning 3 application files.
01:56 PM Uploading 1 files and blobs.
01:56 PM Uploaded 1 files and blobs
01:56 PM Compilation starting.
01:57 PM Compilation completed.
01:57 PM Starting deployment.
01:57 PM Checking if deployment succeeded.
01:57 PM Deployment successful.
01:57 PM Checking if updated app version is serving.
01:57 PM Completed update of app: cloud-chess, version: 2
01:57 PM Uploading index definitions.
2013-07-07 13:57:18 (Process exited with code 0)

chessboard.py

import os

import webapp2
import jinja2


JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'])

class MainHandler(webapp2.RequestHandler):
    def get(self):
        template = JINJA_ENVIRONMENT.get_template('files/html/chess.html')
        self.response.write(template.render())

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

的app.yaml

application: cloud-chess
version: 2
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: /html
  static_dir: files/html/

- url: /images
  static_dir: files/images/

- url: /scripts
  static_dir: files/scripts/

- url: .*
  script: chessboard.app

libraries:
- name: webapp2
  version: "2.5.2"
- name: jinja2
  version: latest

2 个答案:

答案 0 :(得分:2)

您的问题是您尝试加载的模板位于静态指令html中。

不需要将模板部署为静态资源 - 实际上您可能不应该这样做。

部署静态资源时,应用程序通常无法读取这些资源。 最近添加了app.yaml指令,以便您阅读这些资源。

除非您希望静态提供html文件,否则只需删除/html静态指令,或将模板移到其他位置。

或添加指令。 application_readable/html静态处理程序

请参阅静态处理程序的文档https://developers.google.com/appengine/docs/python/config/appconfig#Static_Directory_Handlers

就我个人而言,我建议不要静态地提供jinja模板以及使用它们进行渲染。

为什么这在开发环境中有效 - 开发环境不使用不同的存储机制来提供静态和应用程序可读资源。

答案 1 :(得分:0)

您无法将模板存储在静态目录中。

只需从app.yaml中删除/html处理程序,如下所示:

application: cloud-chess
version: 2
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: /images
  static_dir: files/images/

- url: /scripts
  static_dir: files/scripts/

- url: .*
  script: chessboard.app

libraries:
- name: webapp2
  version: "2.5.2"
- name: jinja2
  version: latest