模板不存在于 /

10 投票
5 回答
35854 浏览
提问于 2025-04-18 01:41

你好!这里有很多帖子标题相同,但没有一个解决我的问题。我有一个Django网站,可以访问/admin(不过看起来不太好)。但是在/路径下出现了以下错误页面(DEBUG = Truesettings.py中):

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/页面现在也看起来不错。

非常感谢大家的建议。

5 个回答

1

如果以后有人再看到这个,似乎你只需要这样做:

from django.shortcuts import render


# Create your views here.
def index(request):
    # I guess for some reason you don't need to tell that it is in the templates folder anymore
    return render(request, 'home.html')

你只需要在settings.py文件里的已安装应用(这里是“core”)中写上应用的名字就可以了。

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core'
]
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')

7

可能的原因是,Django正在尝试在包含你的settings.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,
)

这个错误通常是因为模板文件夹的配置不正确。

10

我花了很多时间在搞清楚我的 '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': [],

24

从你展示的内容来看,我觉得你的项目结构可能有问题。

通常,项目的结构应该是这样的:

├── 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 中的模板,你需要在你的 INSTALLED_APPS 设置里添加 yourproject

这样讲清楚了吗?如果还不够,可以编辑一下你原来的帖子,发一下你的项目结构吗?

撰写回答