GAE模板不起作用

时间:2012-03-17 21:12:56

标签: python django google-app-engine templates

我在settins.py中有以下代码:

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__),'html').replace("\\","/"),
)

并在请求处理程序中:

r = template.render('mt.html', {'some_content':blabla,})

我期待,该模板将从/project_dir/html/mt.html文件中加载。 但它失败并出现以下错误:

Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\_webapp25.py", line 701, in __call__
handler.get(*groups)
File "D:\ap\pz4\pz4\main.py", line 33, in get
x8= template.render(fn, {'some_content':blabla,})
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\template.py", line 91, in render
t = _load_user_django(template_path, debug)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\template.py", line 113, in _load_user_django
template = django.template.loader.get_template(file_name)
File "C:\Program Files (x86)\Google\google_appengine\lib\django_1_3\django\template\loader.py", line 157, in get_template
template, origin = find_template(template_name)
File "C:\Program Files (x86)\Google\google_appengine\lib\django_1_3\django\template\loader.py", line 138, in find_template
raise TemplateDoesNotExist(name)
TemplateDoesNotExist: mt.html

同时,当我使用直接文件夹定义调用它时,它工作正常:

r = template.render(os.path.join(os.path.dirname(__file__),'html/mt.html').replace("\\","/"),{'some_content':blabla,})

GAE是1.6.3(本地),django版本(使用use_library('django','xxx'))检查0.96,1.2和1.3,结果是相同的。

我做错了什么?

4 个答案:

答案 0 :(得分:1)

因此,webapp引擎中的问题:“ _load_user_django ”方法(来自我系统上的C:\ Program Files(x86)\ Google \ google_appengine \ google \ appengine \ ext \ webapp) 覆盖用户定义的TEMPLATE_DIRS变量:

def _load_user_django(path, debug):
  """Load the given template using the django found in third_party."""
  abspath = os.path.abspath(path)

  if not debug:
    template = template_cache.get(abspath, None)
  else:
    template = None

  if not template:
    directory, file_name = os.path.split(abspath)
    new_settings = {
        'TEMPLATE_DIRS': (directory,),
        'TEMPLATE_DEBUG': debug,
        'DEBUG': debug,
        }

    old_settings = _swap_settings(new_settings)
    ...

因此,当 get_template_sources 方法(来自filesystem.py)尝试调用“safe_join(template_dir,template_name)”时,它会失败,并显示以下错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\admin\__init__.py", line 317, in post
    exec(compiled_code, globals())
  File "<string>", line 3, in <module>
  File "C:\Program Files (x86)\Google\google_appengine\lib\django_1_3\django\utils\_os.py", line 46, in safe_join
    'path component (%s)' % (final_path, base_path))
ValueError: The joined path (x:\app_path\html\mt.html) is located outside of the base path component (x:\app_path\html\sub_templ_folder)

答案 1 :(得分:0)

默认情况下,django在app_name / templates / app_name / template.html中查找模板(是的,app_name重复)

所以看起来在你的情况下,它会在project_dir / html / project_dir / template.html中寻找一个模板,假设project_dir与你的django应用程序名称相同。

答案 2 :(得分:0)

有库存GAE,不使用Django的TEMPLATE_DIRS设施。我执行以下操作,基本上直接来自the templates docs

path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))

在这种情况下,'index.html'是生活在应用程序根目录下的资源。如果您的模板位于应用内的目录中,请相应地调整os.path.join()

答案 3 :(得分:0)

检查您的yaml文件中是否包含以下行

- url: /templates
  static_dir: templates

如果你删除这两行,你的代码将正常工作,假设你的路径如下:

path = os.path.join(os.path.dirname(__file__), 'templates/home.html')