GAE模板没有

2024-04-27 05:24:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的集合.py公司名称:

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

在请求处理程序中:

^{pr2}$

我希望,模板将从/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,结果是一样的。在

我做错什么了?在


Tags: pathdjangoinpyoshtmlgoogleline
3条回答

在stock 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))

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

因此,webapp引擎中的问题是:“\u 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方法(从文件系统.py)尝试调用“safe_join(template_dir,template_name)”,但失败,并出现以下错误:

^{pr2}$

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

所以在您的例子中,它会在project\u dir/html/project\u dir中查找模板/模板.html,假设project\u dir与您的django应用程序名称相同。在

相关问题 更多 >