【问题标题】:GAE templates doesn't workGAE 模板不起作用
【发布时间】:2012-03-17 21:12:55
【问题描述】:

我的 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检查,结果是一样的。

我做错了什么?

【问题讨论】:

    标签: python django google-app-engine templates


    【解决方案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)
    

    【讨论】:

      【解决方案2】:

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

      所以在您的情况下,它会在 project_dir/html/project_dir/template.html 中查找模板,假设 project_dir 与您的 django 应用名称相同。

      【讨论】:

      • 我将“mt.html”放入以下文件夹:app_root app_root/html app_root/html/app_name 但它仍然无法正常工作。
      • 这些文档对您的帮助可能比我自己多一点。 docs.djangoproject.com/en/dev/ref/templates/api 看起来我走错了路,我在模板中使用了子文件夹,但你没有这样做。通过添加一行来在堆栈跟踪中指定的适当位置打印出模板路径或文件名,应该很容易调试。
      【解决方案3】:

      库存 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()

      【讨论】:

      • 很明显,它也适用于我,但我需要使用“扩展模板”功能,并且只有当所有模板都存储在同一个(根)文件夹中时它才有效。但我有很深(2-3 级)的文件夹结构,不喜欢将大约 40 个模板存储到同一个文件夹中。
      • @user1276220:我对相关问题的回答有一种技术允许您在extendsinclude 调用中使用相对路径:stackoverflow.com/questions/5263623/…
      【解决方案4】:

      检查您的 yaml 文件中是否没有以下行

      - url: /templates
        static_dir: templates
      

      如果您删除这两行代码,假设您的路径如下:

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

      【讨论】:

        猜你喜欢
        • 2012-07-25
        • 2014-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多