Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.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
Python GAE模板不';行不通_Python_Django_Google App Engine_Templates - Fatal编程技术网

Python GAE模板不';行不通

Python GAE模板不';行不通,python,django,google-app-engine,templates,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 rec

我的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
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)
同时,当我使用直接文件夹定义调用它时,它工作正常:

r = template.render(os.path.join(os.path.dirname(__file__),'html/mt.html').replace("\\","/"),{'some_content':blabla,})
GAE是1.6.3(本地),django版本(使用U库('django','xxx'))与0.96、1.2和1.3进行检查,结果相同


我做错了什么?

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


因此,在您的案例中,它将在project_dir/html/project_dir/template.html中查找模板,假设project_dir与您的django应用程序名称相同。

在库存GAE中,django的
模板_DIRS
功能未使用。我做了以下工作,这基本上是直接的:


其中,在本例中,“index.html”是位于应用程序根目录下的资源。如果您的模板位于应用程序内的目录中,请相应地调整
os.path.join()

因此,webapp引擎中的问题是“\u load\u user\u django”方法(在我的系统上从C:\Program Files(x86)\Google\Google\u appengine\Google\appengine\ext\webapp中)覆盖用户定义的模板目录变量:

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\u template\u sources方法(从filesystem.py)尝试调用“safe\u join(template\u dir,template\u name)”时,失败并出现以下错误:

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
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)
回溯(最近一次呼叫最后一次):
文件“C:\Program Files(x86)\Google\Google\u appengine\Google\appengine\ext\admin\\uuuuuuu init\uuuuuuuuu.py”,第317行,在post中
exec(编译的_代码,globals())
文件“”,第3行,在
文件“C:\Program Files(x86)\Google\Google\u appengine\lib\django\u 1\u 3\django\utils\\u os.py”,第46行,在safe\u join中
“路径组件(%s)”(最终路径,基本路径))
ValueError:连接的路径(x:\app\u path\html\mt.html)位于基本路径组件(x:\app\u path\html\sub\u temp\u文件夹)之外

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

- url: /templates
  static_dir: templates
如果删除这两行代码,则假定路径如下所示,代码将正常工作:

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

我把“mt.html”放在以下文件夹中:app_root app_root/html app_root/html/app_name,但它仍然不起作用。这些文档可能会给你带来比我更多的帮助。看起来我走错了方向,我在模板中使用了子文件夹,但你没有这样做。通过添加一行在堆栈跟踪中指定的适当位置打印模板路径或文件名,调试应该很容易。很明显,这对我也适用,但我需要使用“扩展模板”功能,并且只有当所有模板都存储在同一个(根)文件夹中时,它才起作用。但是我有很深的(2-3级)文件夹结构,不喜欢在同一个文件夹中存储约40个模板。@user1276220:我对相关问题的回答有一种技巧,允许您在
扩展
包含
调用中使用相对路径: