2012-03-17 96 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')