2012-06-01 3 views
1

내가 Jinja2와 구글 앱 엔진에 TemplateNotFound 오류 받고 있어요와 (템플릿) (아래 전체 스택 추적.)TemplateNotFound 구글 앱 엔진 및 Jinja2

은 내가 볼 것으로 예상하기로 index.html을입니다 " greet "변수를 index.html 템플리트 파일로 전달합니다. 내가 이해할 수없는 것은 TraceBack의 index.html 경로가 올바른 경우 템플릿을 찾을 수 없다는 오류가 발생하는 이유입니다. 나는 무엇을 시도했다

은 ...

  • 템플릿 경로에 "os.path.dirname (파일)"을 복용하여 상대 경로를 시도했다.
  • 테마 대신 "템플리트"를 디렉토리 이름으로 사용하십시오.

여기 내 코드입니다.

애플리케이션 제목

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) 

config.py

# 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 
+0

index.html 템플릿 파일의 절대 경로는 무엇입니까? – proppy

답변

0

당신이 C 보면 : \ 사용자 \ 존 \ 경우 Webdev \ 구글 \ lib 디렉토리 \ jinja2 \ jinja2 \ loaders.py 라인 (33) 당신은 아마 PDB 중단 import pdb; pdb.set_trace()을 설정하고 무슨 일이 일어나고 있는지 볼 수 있습니다.

난 맥에 SDK 소스 배포판을 사용하고 있지만, 코드는 내가 무엇을보고 같은 경우, 그것은 : 그것은에 '/'대신 path.sep 분할

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 

참고 for piece in template.split('/') . 나는 당신의 입력이 C : \ Path \ to \ file.html인데, 어떤 경우에는 전체 경로가 adn path.sep (\ windows에 있음)가 실제로 조각에 들어있어 TemplateNotFound 오류가 발생했다고 생각합니다. 대신 template_name = os.path.join("admin", template_name)

당신이 template_path에서 TEMPLATE_NAME을 포함하지 않는, template_name = 'admin/%s' % template_name을 시도하고 loaders.py 라인 (162)으로 env.get_template(template_name, template_vals or {})로 대신 template_path의 TEMPLATE_NAME을 전달할 수는 수정, template_path

코드에서 TEMPLATE_NAME에 대한 조회한다는 것을 의미합니다 하지만 검증되지 않은 내가 당신의 정적 파일과 같은 경로에서 신사 템플릿을로드하려고하지 희망

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) 
1

처럼 보일 것이다

static_files : template/\ 1/static/\ 2

해당 경로는 애플리케이션에서 액세스 할 수 없기 때문에

Jinja가 템플리트를로드하려고 시도하는 경로를 기록하려고합니다.이 템플리트를로드하려고하는 위치를 이해하는 데 도움이됩니다.