2011-06-05 32 views
0

好的。我已經閱讀了許多關於這種情況的其他記錄,並且當提問者正確修改了TEMPLATE_DIRS設置時,他們總是得到解決。這些建議都不適合我。奇怪的是,使用Django Web服務器時會加載模板,但使用mod_wsgi時會出錯。我認爲django可能會拋出一個不正確的異常,因爲TemplateDoesNotExist錯誤根本無法應用給定我的設置。Django對我說謊嗎?!當文件確實存在時,render_to_response函數返回TemplateDoesNotExist

我的代碼:

我的應用程序/視圖/ view.py(是的,我有我的子文件夾視圖)

8 def index(request): 
    9  return render_to_response('pages/home/base_home.html') 

這裏是我的settings.py位於根(標準位置)

106 TEMPLATE_DIRS = (
107  # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
108  # Always use forward slashes, even on Windows. 
109  # Don't forget to use absolute paths, not relative paths. 
110  "/home/foobar/templates", 
111) 

最後,這是我的看法位於/家庭/ foobar的/模板/頁/家庭/

1 {% extends 'base.html' %} 
    2 
    3 {% block navigation %} 
    4  {% include 'modules/navigation/navigation.html' %} 
    5 {% endblock %} 
    6 
    7 {% block slideshow %} 
    8  {% include 'modules/slideshow/slideshow.html' %} 
    9 {% endblock %} 

而且,這裏是一個ls -l命令,證明該文件的確存在

[email protected]:~$ ls -l templates/pages/home/ 
total 12 
-rwxr-xr-x 1 foo foo 205 2011-06-05 12:37 base_home.html 
[email protected]:~$ 

這裏是屍檢報告的Django給我:

Template-loader postmortem 

Django tried loading these templates, in this order: 
Using loader django.template.loaders.filesystem.Loader: 
/home/foobar/templates/pages/home/base_home.html (File does not exist) 
Using loader django.template.loaders.app_directories.Loader: 
/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templates/pages/home/base_home.html (File does not exist) 

編輯

這裏我的wsgi文件

3 import os 
    4 import sys 
    5 
    6 path = '/media/PENDRIVE/Projects/' 
    7 if path not in sys.path: 
    8  sys.path.append(path) 
    9  
10 os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' 
11 
12 import django.core.handlers.wsgi 
13 application = django.core.handlers.wsgi.WSGIHandler() 
14 

謝謝你的任何見解,因爲我感到困惑和困惑!

回答

0

您應該檢查模板文件夾是否可供用戶運行的wsgi進程讀取。我想這是一個簡單的權限問題。

+0

喜BASE_DIR的完整路徑來解決。感謝您的建議。我的權限是:drwxr-xr-x 9 foo foo 4096 2011-06-05 12:57模板。我將其改爲可讀取給大家進行調試。仍然不起作用。 – 2011-06-05 20:49:07

+0

模板文件夾本身並不一定使wsgi進程可讀。如果例如/ home/foobar只能被foobar讀取,那麼這仍然不起作用。 – 2011-06-05 20:58:21

+0

@mu是的,我做到了。爲了測試,我遞歸地chmod 755'd整個項目目錄和templates目錄。任何用戶都可以讀取和執行所有文件。 (還是)感謝你的建議。 – 2011-06-05 20:58:47

0

您沒有包含正確的網址。它應該是「網頁/家庭/模塊/導航/ navigation.html」

+0

嗨。謝謝,但這並不能解決問題。我已經嘗試用'hello world'替換模板的內容。我仍然得到相同的錯誤告訴我:/home/foobar/templates/pages/home/base_home.html(文件不存在)。謝謝你! – 2011-06-05 21:07:49

0

這應該通過替換settings.py

TEMPLATES = [ 
{ 
    'BACKEND': 'django.template.backends.django.DjangoTemplates', 
    'DIRS': ['/var/www/assistance/templates','templates'], 
    'APP_DIRS': True, 
    'OPTIONS': { 
     'context_processors': [ 
      'django.template.context_processors.debug', 
      'django.template.context_processors.request', 
      'django.contrib.auth.context_processors.auth', 
      'django.contrib.messages.context_processors.messages', 
     ], 
    }, 
}, 
]