【问题标题】:TemplateDoesNotExist at /模板不存在于 /
【发布时间】:2014-05-19 11:52:39
【问题描述】:

嘿嘿!这里的许多线程都有相同的标题,但没有一个能解决我的问题。我有一个可以访问 /admin 的 Django 站点(但它看起来很难看)。但是在/出现以下错误页面(settings.py中的DEBUG = True):

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。但是当我在我的网络浏览器中加载页面时,它并没有被创建。这是否意味着 settings.py 永远不会加载? Django怎么能说,一个存在的文件不存在呢?

编辑¹:
我的 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 + (
)

编辑²:

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

编辑³:
好的。这现在为我解决了。解决方案是不同事物的组合。问题之一是权利缺失。执行 Django 的用户无法列出 templates/ 目录的内容。 chmod o+x templates/ 完成了这项工作。然后在settings.py 中有一些设置,将查找模板的位置从templates/iecl_dev/ 更改为iecl_dev/templates/。我在我的网络浏览器的错误消息中看到了这个错误的路径。但仅仅将 settings.py 恢复到旧版本是不够的。某些服务需要重新启动。我只是重新启动了机器,一切都很好。神奇的是,/admin/ 页面现在看起来也不错。

非常感谢您提供的所有提示。

【问题讨论】:

标签: python django


【解决方案1】:

我知道这已经晚了,但这里提到的解决方案对我不起作用。所以这是有效的:

  1. settings.py 文件中,将您的appConfig 添加到已安装的应用中。
INSTALLED_APPS = [ '...', 'AppName.apps.AppNameConfig', ]
  1. 创建一个模板目录appName/templates/appName。 (其中 appName 是您的应用的名称)。

  2. 当你调用你的渲染函数时,你可以传递它templates.name.html。像这样:

return render(request, 'templates/blog.html')

【讨论】:

    【解决方案2】:

    我浪费了很多时间试图找出我的'templates' 目录出了什么问题,并弄清楚了:

    您可能忘记在settings.py 的以下部分中添加TEMPLATE_DIR

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            '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',
                ],
            },
        },
    ]
    

    所以它应该有'DIRS': [TEMPLATE_DIR,], 而不是'DIRS': [],

    【讨论】:

    • 这解决了我的问题!感谢您的发布!完全忘记添加这个目录了。
    • @user3408397 Lmao np 这很简单,但我也浪费了很多时间。
    【解决方案3】:

    原因可能是 django 试图在您的 setting.py 文件目录中查找您的模板目录 而实际上模板目录位于父目录中。 尝试在您的 settings.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,
    )
    

    该错误通常是由于模板目录配置不正确

    【讨论】:

      【解决方案4】:

      从您所展示的内容来看,我认为您的项目布局可能存在问题。

      通常设置如下:

      ├── 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 设置中。

      有帮助吗?如果没有,您可以使用您的项目布局编辑您的原始帖子吗?

      【讨论】:

      • 好的。听起来不错。但我的布局与此完全不同(见上文)。我不是本网站的作者,无法说出原因。因此,当我的 manage.py 和我的 settings.py 位于 . 中时,模板位于 ../templates/iecl_dev/ 中。我现在应该向 INSTALLED_APPS 添加什么?
      • 由于您的布局与我通常所知道的确实不同,我不知道您如何使其工作。也许你可以试试@Ishan070692 的答案。如果您的网站不是太大并且没有理由使用这种特定布局,我建议您按照Django Documentation 的建议重构您的项目。它将简化您的开发过程,人们将能够了解您的项目并更快地为您提供帮助。
      • 感谢您的出色解释和解决方案!我遇到了类似的问题,这不仅解决了它,而且使它变得有意义。
      • 完美!这解决了我的问题!谢谢!
      猜你喜欢
      • 2011-08-06
      • 2015-07-23
      • 2013-05-28
      • 2014-03-10
      • 2017-01-23
      相关资源
      最近更新 更多