2014-04-07 170 views
5

Hej!這裏的許多線程都有相同的標題,但沒有一個能解決我的問題。我有一個Django網站可以訪問/管理(但它看起來很醜陋)。但在/有出現以下錯誤頁面(DEBUG = Truesettings.py):TemplateDoesNotExist at/

TemplateDoesNotExist at/

index.html 

Request Method:  GET 
Request URL: http://iecl.uni-potsdam.de/ 
Django Version:  1.4.5 
Exception Type:  TemplateDoesNotExist 
Exception Value:  

index.html 

Exception Location:  /usr/lib/python2.7/dist-packages/django/template/loader.py in find_template, line 138 
Python Executable: /usr/bin/python 
Python Version:  2.7.3 
Python Path:  

['/home/python/iecl/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg', 
'/home/python/iecl/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-linux2', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages', 
'/usr/lib/pymodules/python2.7', 
'/home/python/iecl/lib/python2.7/site-packages', 
'/home/django/django'] 

Server time: Mon, 7 Apr 2014 11:28:46 +0200 
Template-loader postmortem 

Django tried loading these templates, in this order: 

    Using loader django.template.loaders.filesystem.Loader: 
     /home/django/django/templates/iecl_dev/index.html (File does not exist) 
    Using loader django.template.loaders.app_directories.Loader: 
     /usr/lib/python2.7/dist-packages/django/contrib/auth/templates/index.html (File does not exist) 
     /usr/lib/python2.7/dist-packages/django/contrib/admin/templates/index.html (File does not exist) 

事實上,該文件/home/django/django/templates/iecl_dev/index.html確實存在,我也試過chmod o+r index.html沒有成功。

python iecl_dev/manage.py runserver 0.0.0.0:0輸出是

Validating models... 

0 errors found 
Django version 1.4.5, using settings 'iecl_dev.settings' 
Development server is running at http://0.0.0.0:0/ 
Quit the server with CONTROL-C. 

所以一切都似乎在這裏很好。

讓我困惑的是:* .pyc文件是在* .py文件運行時自動創建的,對吧?在python iecl_dev/manage.py runserver 0.0.0.0:0之後創建了一個文件/home/django/django/iecl_dev/settings.pyc。但是當我在我的Web瀏覽器中加載頁面時,它不會被創建。這是否意味着,settings.py永遠不會被加載? Django怎麼說,一個存在的文件將不存在?

Edit¹:
我的settings.py如下所示:

import django.conf.global_settings as DEFAULT_SETTINGS 
import os 
DEBUG = True 
TEMPLATE_DEBUG = DEBUG 

ADMINS = (
) 

SETTINGS_PATH = os.path.realpath(os.path.dirname(__file__)) 

MANAGERS = ADMINS 
DATABASES = {                                       $ 
    'default': {                                      $ 
     'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 
     'NAME': 'iecl',      # Or path to database file if using sqlite3. 
       'USER': 'iecl',      # Not used with sqlite3. 
     'PASSWORD': '<xxx>',     # Not used with sqlite3. 
     'HOST': '',      # Set to empty string for localhost. Not used with sqlite3.                 $ 
     'PORT': '',      # Set to empty string for default. Not used with sqlite3. 
    } 
} 
TIME_ZONE = 'Europe/Berlin' 

LANGUAGE_CODE = 'en-us' 

SITE_ID = 1 

USE_I18N = True 

MEDIA_ROOT = '/var/www/django_media/iecl_dev/media/' 
MEDIA_URL = '' 

ADMIN_MEDIA_PREFIX = '/media/admin_media/' 

SECRET_KEY = '<xxx>' 

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader', 
    'django.template.loaders.app_directories.Loader', 
) 

MIDDLEWARE_CLASSES = (
     'django.middleware.csrf.CsrfViewMiddleware', 
     'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
     'django.contrib.messages.middleware.MessageMiddleware', 
) 

ROOT_URLCONF = 'iecl_dev.urls' 

TEMPLATE_DIRS = (
     os.path.join(SETTINGS_PATH, 'templates'), 
) 
INSTALLED_APPS = (
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
     'iecl_dev.showStudents', 
     'django.contrib.admin', 
) 

TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
) 

Edit²:

The contents of `/home/django/django/` are as follows: 

/home/django/django/: 
iecl2 
iecl_dev 
templates 

/home/django/django/iecl2: 
__init__.py 
__init__.pyc 
manage.py 
settings.py 
settings.pyc 
showStudents 
urls.py 
urls.pyc 

/home/django/django/iecl2/showStudents: 
__init__.py 
__init__.pyc 
admin.py 
context_processors.py 
models.py 
models.pyc 
views.py 
views.pyc 

/home/django/django/iecl_dev: 
__init__.py 
__init__.pyc 
manage.py 
piwik 
settings.py 
settings.pyc 
showStudents 
urls.py 
urls.pyc 

/home/django/django/iecl_dev/piwik: 
__init__.py 
app_settings.py 
context_processors.py 
models.py 
tests.py 
urls.py 
views.py 

/home/django/django/iecl_dev/showStudents: 
__init__.py 
__init__.pyc 
admin.py 
context_processors.py 
models.py 
models.pyc 
views.py 

/home/django/django/templates: 
iecl 
iecl_dev 
/home/django/django/templates/iecl: 
500.html 
admin 
changePW.html 
editStudent.html 
feedback.html 
feedback_thanks.html 
index.html 
location.html 
login.html 
page.html 
password_changed.html 
showStudent.html 
studentsOverview.html 

/home/django/django/templates/iecl/admin: 
404.html 
500.html 
actions.html 
app_index.html 
auth 
base.html 
base_site.html 
change_form.html 
change_list.html 
change_list_results.html 
date_hierarchy.html 
delete_confirmation.html 
delete_selected_confirmation.html 
edit_inline 
filter.html 
includes 
index.html 
invalid_setup.html 
login.html 
object_history.html 
pagination.html 
prepopulated_fields_js.html 
search_form.html 
showStudents 
submit_line.html 

/home/django/django/templates/iecl/admin/auth: 
user 

/home/django/django/templates/iecl/admin/auth/user: 
add_form.html 
change_password.html 

/home/django/django/templates/iecl/admin/edit_inline: 
stacked.html 
tabular.html 

/home/django/django/templates/iecl/admin/includes: 
fieldset.html 

/home/django/django/templates/iecl/admin/showStudents: 
pagecontent 
userpagecontent 

/home/django/django/templates/iecl/admin/showStudents/pagecontent: 
change_form.html 

/home/django/django/templates/iecl/admin/showStudents/userpagecontent: 
change_form.html 

/home/django/django/templates/iecl_dev: 
500.html 
__init__.py 
admin 
changePW.html 
editStudent.html 
feedback.html 
feedback_thanks.html 
index.html 
location.html 
login.html 
page.html 
password_changed.html 
piwik 
showStudent.html 
studentsOverview.html 

/home/django/django/templates/iecl_dev/admin: 
404.html 
500.html 
actions.html 
app_index.html 
auth 
base.html 
base_site.html 
change_form.html 
change_list.html 
change_list_results.html 
date_hierarchy.html 
delete_confirmation.html 
delete_selected_confirmation.html 
edit_inline 
filter.html 
includes 
index.html 
invalid_setup.html 
login.html 
object_history.html 
pagination.html 
prepopulated_fields_js.html 
search_form.html 
showStudents 
submit_line.html 

/home/django/django/templates/iecl_dev/admin/auth: 
user 

/home/django/django/templates/iecl_dev/admin/auth/user: 
add_form.html 
change_password.html 

/home/django/django/templates/iecl_dev/admin/edit_inline: 
stacked.html 
tabular.html 

/home/django/django/templates/iecl_dev/admin/includes: 
fieldset.html 

/home/django/django/templates/iecl_dev/admin/showStudents: 
pagecontent 
userpagecontent 

/home/django/django/templates/iecl_dev/admin/showStudents/pagecontent: 
change_form.html 

/home/django/django/templates/iecl_dev/admin/showStudents/userpagecontent: 
change_form.html 

/home/django/django/templates/iecl_dev/piwik: 
tracking.html 

Edit³:
確定。現在我解決了這個問題。解決方案是一個不同事物的綜合體。其中一個問題是失去的權利。執行Django的用戶無法列出模板/目錄的內容。 chmod o+x templates/完成了這項工作。然後在settings.py中有一些設置,它改變了從模板/ iecl_dev /到iecl_dev/templates /尋找模板的地方。我在我的Web瀏覽器中看到了錯誤消息中的錯誤路徑。但簡單地將settings.py恢復到舊版本是不夠的。一些服務需要重新啓動。我只需重新啓動機器,一切都很好。奇蹟般地,/ admin /頁面現在看起來不錯。

非常感謝您的所有提示。

+1

在哪裏你想使你的模板。 –

+0

如果尚未修復,請參閱我的回答, http://stackoverflow.com/a/32358905/303681 – mmrs151

回答

12

從您所展示的內容來看,我認爲您的項目佈局可能存在問題。

├── manage.py 
├── yourproject 
│   ├── __init__.py 
│   ├── settings.py 
│   ├── templates 
│   ├── urls.py 
│   ├── wsgi.py 
│   └── wsgi.pyc 
├── yourfirstapp 
│   ├── __init__.py 
│   ├── admin.py 
│   ├── models.py 
│   ├── templates 
│   ├── tests.py 
│   ├── urls.py 
│   └── views.py 
├── yoursecondapp 
    ├── __init__.py 
    ├── admin.py 
    ├── models.py 
    ├── templates 
    ├── tests.py 
    ├── urls.py 
    └── views.py 

正如你所看到的,你的項目中的每個應用程序可以有它自己的templates目錄:

通常而言,如下設置。 yourproject有點特別,因爲它還存儲唯一文件,如settings.pywsgi.py。但是,您可以考慮並將其用作應用程序。

現在,如果要使用存儲在yourproject/templates中的模板,則必須將yourproject添加到INSTALLED_APPS設置中。

它有幫助嗎?如果沒有,你可以編輯你的原始文章與你的項目佈局?

+0

好的。聽起來不錯。但我的佈局完全不同於此(參見上文)。我不是這個網站的作者,也不能說出理由。因此,當我的manage.py和我的settings.py文件位於時,那麼這些模板位於../templates/iecl_dev/中。我現在應該添加到INSTALLED_APPS? – user3506025

+0

由於您的佈局與我通常所知道的完全不同,因此我不知道如何使其工作。也許你可以試試@ Ishan070692的anwser。如果你的網站不是太大,而且沒有理由爲這個特定的佈局,我建議你按照[Django文檔]重構你的項目(https://docs.djangoproject.com/en/1.6/intro/tutorial01/ )的建議。它將減輕你的開發過程,人們將能夠理解你的項目並幫助你更快。 –

+0

感謝您的這個偉大的解釋和解決方案!我遇到了類似的問題,這不僅解決了問題,而且使問題變得有意義。 – Synthead

5

其原因可能是,django試圖在您的setting.py文件中查找您的模板目錄 而實際上模板目錄位於父目錄中。 嘗試在你的設置中做這樣的事情。PY

SETTINGS_PATH = os.path.dirname(__file__) 
PROJECT PATH = os.path.join(SETTINGS_PATH, os.pardir) 
PROJECT_PATH = os.path.abspath(PROJECT_PATH) 
TEMPLATES_PATH = os.path.join(PROJECT_PATH, "templates") 

TEMPLATE_DIRS = (
    TEMPLATES_PATH, 
) 

的錯誤通常是由於配置不正確的模板目錄