2012-03-17 100 views
0

我在settins.py下面的代码:GAE模板不起作用

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

我在做什么错了?

回答

0

默认的Django会在APP_NAME /模板/ APP_NAME/template.html模板(是的,APP_NAME重复)

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

+0

我把 “mt.html” 到下列文件夹: APP_ROOT APP_ROOT/HTML APP_ROOT/HTML/APP_NAME 但它仍然没有工作。 – user1276220 2012-03-17 22:47:48

+0

文档可能会帮助你比我更多。 https://docs.djangoproject。com/en/dev/ref/templates/api/ 它看起来像我在错误的轨道上,我在我的模板中使用子文件夹,但你没有这样做。通过添加一行来在堆栈跟踪中指定的适当位置打印出template_path或file_name,应该非常容易进行调试。 – dragonx 2012-03-18 02:32:06

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

+0

很明显,它也适用于我,但我需要使用“扩展模板”功能,并且只有当所有模板都存储在同一个(根)文件夹中时,它才能工作。 但我有深层(2-3级)的文件夹结构,不喜欢将〜40个模板存储到同一个文件夹。 – user1276220 2012-03-18 08:34:02

+0

@ user1276220:我在相关问题上的回答有一种技巧,可以让你在'extends'和'include'调用中使用相对路径:http://stackoverflow.com/questions/5263623/templatedoesnotexist-on-python-app - 发动机 - Django的1-2,而模板渲染-RE – 2012-03-18 09:34:03

1

所以,在Web应用程序引擎的问题: “_load_user_django” 的方法(从C:\ Program Files文件(x86)的\谷歌\ google_appengine \我的系统上谷歌\ AppEngine上\分机\ web应用程序)覆盖用户-defined 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) 
0

检查,如果你没有在你的YAML文件中的以下行

- url: /templates 
    static_dir: templates 

如果去掉这两行代码会工作得很好,假设你的路径如下所示:

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