Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine TemplateNotFound(模板)与谷歌应用程序引擎&;金甲2号_Google App Engine_Jinja2 - Fatal编程技术网

Google app engine TemplateNotFound(模板)与谷歌应用程序引擎&;金甲2号

Google app engine TemplateNotFound(模板)与谷歌应用程序引擎&;金甲2号,google-app-engine,jinja2,Google App Engine,Jinja2,我在使用Jinja2的Google应用程序引擎上发现一个TemplateNotFound错误(下面是完整的堆栈跟踪) 我希望看到的是index.html,其中包含传递到index.html模板文件的“greet”变量。我不明白的是,当回溯中index.html的路径正确时,为什么会出现模板未找到错误 我试过的 通过在模板路径中去掉“os.path.dirname(文件)”尝试了相对路径 使用“模板”而不是主题作为目录名 这是我的密码 应用程序yaml application: codemyw

我在使用Jinja2的Google应用程序引擎上发现一个TemplateNotFound错误(下面是完整的堆栈跟踪)

我希望看到的是index.html,其中包含传递到index.html模板文件的“greet”变量。我不明白的是,当回溯中index.html的路径正确时,为什么会出现模板未找到错误

我试过的

  • 通过在模板路径中去掉“os.path.dirname(文件)”尝试了相对路径
  • 使用“模板”而不是主题作为目录名
这是我的密码

应用程序yaml

application: codemywayout
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:

- url: /admin/.*
  script: admin.app
  login: admin

- url: /static/([^/]+)/(.*)
  static_files: template/\1/static/\2
  upload: static/.*

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

- url: .*
  script: static.app

builtins:
- remote_api: on

libraries:
- name: webapp2
  version: "2.5.1"
- name: jinja2
  version: latest
admin.py

from google.appengine.ext import db
import webapp2
import jinja2
import os
import fix_path
import config


def render_template(template_name, template_vals=None, theme=None):
    template_path = os.path.join(os.path.dirname(__file__) , \
            "themes", theme or config.theme, template_name)
    env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(template_path))
    return env.get_template(template_path, template_vals or {})

class BlogPost(db.Model):
    title = db.StringProperty()
    body = db.StringProperty()

def render(self):
    template_vals = {
        'config': config,
        'post': self,
    }
    return render_template("post.html", template_vals)

class BaseHandler(webapp2.RequestHandler):
    def render_to_response(self, template_name, \
            template_vals=None, theme=None):
        template_name = os.path.join("admin", template_name)
        self.response.out.write(render_template(template_name,\
            template_vals, theme))

class AdminHandler(BaseHandler):
    def get(self):
        greet = "hello"
        template_vals = {
            'greet': greet
        }
        self.render_to_response("index.html", template_vals)
# Name of the blog
blog_name = 'My Blog'

# Selects the theme to use. Theme names correspond to directories under
# the 'themes' directory, containing templates and static content.
theme = 'default'

# Defines the URL organization to use for blog postings. Valid substitutions:
#   slug - the identifier for the post, derived from the title
#   year - the year the post was published in
#   month - the month the post was published in
#   day - the day the post was published in

# URL Options
#   post_path_format = '/%(year)d/%(month)02d/%(slug)s'
post_path_format = '/%(slug)s'
config.py

from google.appengine.ext import db
import webapp2
import jinja2
import os
import fix_path
import config


def render_template(template_name, template_vals=None, theme=None):
    template_path = os.path.join(os.path.dirname(__file__) , \
            "themes", theme or config.theme, template_name)
    env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(template_path))
    return env.get_template(template_path, template_vals or {})

class BlogPost(db.Model):
    title = db.StringProperty()
    body = db.StringProperty()

def render(self):
    template_vals = {
        'config': config,
        'post': self,
    }
    return render_template("post.html", template_vals)

class BaseHandler(webapp2.RequestHandler):
    def render_to_response(self, template_name, \
            template_vals=None, theme=None):
        template_name = os.path.join("admin", template_name)
        self.response.out.write(render_template(template_name,\
            template_vals, theme))

class AdminHandler(BaseHandler):
    def get(self):
        greet = "hello"
        template_vals = {
            'greet': greet
        }
        self.render_to_response("index.html", template_vals)
# Name of the blog
blog_name = 'My Blog'

# Selects the theme to use. Theme names correspond to directories under
# the 'themes' directory, containing templates and static content.
theme = 'default'

# Defines the URL organization to use for blog postings. Valid substitutions:
#   slug - the identifier for the post, derived from the title
#   year - the year the post was published in
#   month - the month the post was published in
#   day - the day the post was published in

# URL Options
#   post_path_format = '/%(year)d/%(month)02d/%(slug)s'
post_path_format = '/%(slug)s'
回溯

Traceback (most recent call last):
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Users\john\webdev\google\lib\webapp2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\Users\john\webdev\workspace\codemywayout\admin.py", line 49, in get
    self.render_to_response("index.html", template_vals)
  File "C:\Users\john\webdev\workspace\codemywayout\admin.py", line 34, in render_to_response
    template_vals, theme))
  File "C:\Users\john\webdev\workspace\codemywayout\admin.py", line 14, in render_template
    return env.get_template(template_path, template_vals or {})
  File "C:\Users\john\webdev\google\lib\jinja2\jinja2\environment.py", line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "C:\Users\john\webdev\google\lib\jinja2\jinja2\environment.py", line 693, in _load_template
    template = self.loader.load(self, name, globals)
  File "C:\Users\john\webdev\google\lib\jinja2\jinja2\loaders.py", line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "C:\Users\john\webdev\google\lib\jinja2\jinja2\loaders.py", line 162, in get_source
    pieces = split_template_path(template)
  File "C:\Users\john\webdev\google\lib\jinja2\jinja2\loaders.py", line 33, in split_template_path
    raise TemplateNotFound(template)
TemplateNotFound: C:\Users\john\webdev\workspace\codemywayout\themes\default\admin\index.html

如果查看C:\Users\john\webdev\google\lib\jinja2\jinja2\loaders.py第33行,您可能可以设置pdb断点
导入pdb;设置跟踪()
并查看发生了什么

我在Mac上使用SDK源代码发行版,但如果您的代码与我看到的相同,则为:

def split_template_path(template):
    """Split a path into segments and perform a sanity check.  If it detects
    '..' in the path it will raise a `TemplateNotFound` error.
    """
    pieces = []
    for piece in template.split('/'):
        if path.sep in piece \
           or (path.altsep and path.altsep in piece) or \
           piece == path.pardir:
            raise TemplateNotFound(template)
        elif piece and piece != '.':
            pieces.append(piece)
    return pieces
请注意template.split('/')中工件的
在“/”上拆分,而不是
path.sep
。我怀疑您的输入是C:\Path\to\file.html,在这种情况下,片段是整个路径,adn Path.sep(\on windows)确实在片段中,这会导致TemplateNotFound错误

您可以尝试
template\u name='admin/%s'%template\u name
,而不是
template\u name=os.path.join(“admin”,template\u name)
,不要将template\u name包含在template\u路径中,并将template\u name而不是template\u path传递到
env.get\u template(template\u name,template\u vals或{})
as loaders.py第162行指示它在模板路径中查找模板名称

您的代码经过修改但未经测试

from google.appengine.ext import db
import webapp2
import jinja2
import os
import fix_path
import config


def render_template(template_name, template_vals=None, theme=None):
    template_path = os.path.join(os.path.dirname(__file__) , \
            "themes", theme or config.theme)
    env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(template_path))
    return env.get_template(template_name, template_vals or {})

class BlogPost(db.Model):
    title = db.StringProperty()
    body = db.StringProperty()

def render(self):
    template_vals = {
        'config': config,
        'post': self,
    }
    return render_template("post.html", template_vals)

class BaseHandler(webapp2.RequestHandler):
    def render_to_response(self, template_name, \
            template_vals=None, theme=None):
        template_name = "admin/%s" % template_name
        self.response.out.write(render_template(template_name,\
            template_vals, theme))

class AdminHandler(BaseHandler):
    def get(self):
        greet = "hello"
        template_vals = {
            'greet': greet
        }
        self.render_to_response("index.html", template_vals)

如果查看C:\Users\john\webdev\google\lib\jinja2\jinja2\loaders.py第33行,您可能可以设置pdb断点
导入pdb;设置跟踪()
并查看发生了什么

我在Mac上使用SDK源代码发行版,但如果您的代码与我看到的相同,则为:

def split_template_path(template):
    """Split a path into segments and perform a sanity check.  If it detects
    '..' in the path it will raise a `TemplateNotFound` error.
    """
    pieces = []
    for piece in template.split('/'):
        if path.sep in piece \
           or (path.altsep and path.altsep in piece) or \
           piece == path.pardir:
            raise TemplateNotFound(template)
        elif piece and piece != '.':
            pieces.append(piece)
    return pieces
请注意template.split('/')中工件的
在“/”上拆分,而不是
path.sep
。我怀疑您的输入是C:\Path\to\file.html,在这种情况下,片段是整个路径,adn Path.sep(\on windows)确实在片段中,这会导致TemplateNotFound错误

您可以尝试
template\u name='admin/%s'%template\u name
,而不是
template\u name=os.path.join(“admin”,template\u name)
,不要将template\u name包含在template\u路径中,并将template\u name而不是template\u path传递到
env.get\u template(template\u name,template\u vals或{})
as loaders.py第162行指示它在模板路径中查找模板名称

您的代码经过修改但未经测试

from google.appengine.ext import db
import webapp2
import jinja2
import os
import fix_path
import config


def render_template(template_name, template_vals=None, theme=None):
    template_path = os.path.join(os.path.dirname(__file__) , \
            "themes", theme or config.theme)
    env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(template_path))
    return env.get_template(template_name, template_vals or {})

class BlogPost(db.Model):
    title = db.StringProperty()
    body = db.StringProperty()

def render(self):
    template_vals = {
        'config': config,
        'post': self,
    }
    return render_template("post.html", template_vals)

class BaseHandler(webapp2.RequestHandler):
    def render_to_response(self, template_name, \
            template_vals=None, theme=None):
        template_name = "admin/%s" % template_name
        self.response.out.write(render_template(template_name,\
            template_vals, theme))

class AdminHandler(BaseHandler):
    def get(self):
        greet = "hello"
        template_vals = {
            'greet': greet
        }
        self.render_to_response("index.html", template_vals)

我希望您不要尝试从与静态文件相同的路径加载Jinja模板

静态\u文件:模板/\1/static/\2

因为应用程序无法访问该路径


我将尝试记录Jinja尝试加载模板的路径,以帮助您了解它尝试加载模板的位置。

我希望您不要尝试从与静态文件相同的路径加载Jinja模板

静态\u文件:模板/\1/static/\2

因为应用程序无法访问该路径


我会尝试记录Jinja尝试加载模板的路径,以帮助您了解它尝试加载模板的位置。

index.html模板文件的绝对路径是什么?index.html模板文件的绝对路径是什么?如果可以,请为我提供帮助如果可以的话,请帮我。如果可以的话,请帮我