Python 模板不存在于 /

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22909010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:57:53  来源:igfitidea点击:

TemplateDoesNotExist at /

pythondjango

提问by user3506025

Hej! Many threads here had the same caption, but none of them solved my problem. I have a Django site an can access /admin (but it looks ugly). But on / there appears the following error page (DEBUG = Truein settings.py):

嘿!这里的许多线程都有相同的标题,但没有一个解决了我的问题。我有一个可以访问 /admin 的 Django 站点(但它看起来很难看)。但是在 / 出现以下错误页面(DEBUG = Truein settings.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)

In fact, the file /home/django/django/templates/iecl_dev/index.html does exist and I also tried chmod o+r index.htmlwithout success.

事实上,文件/home/django/django/templates/iecl_dev/index.html 确实存在,我也尝试过chmod o+r index.html没有成功。

The output of python iecl_dev/manage.py runserver 0.0.0.0:0is

的输出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.

so everything seems fine here.

所以这里一切都很好。

What perplexes me: The *.pyc files are created automatically when *.py files are run, right? After python iecl_dev/manage.py runserver 0.0.0.0:0there is a file /home/django/django/iecl_dev/settings.pyccreated. But it is not created, when I load the page in my web browser. Does that mean, the settings.py is never loaded? And how can Django say, a file, which exists, would not exist?

令我困惑的是:*.pyc 文件是在运行 *.py 文件时自动创建的,对吗?之后python iecl_dev/manage.py runserver 0.0.0.0:0有一个文件/home/django/django/iecl_dev/settings.pyc创建。但是当我在我的网络浏览器中加载页面时,它没有被创建。这是否意味着 settings.py 永远不会加载?Django 怎么能说一个存在的文件就不存在呢?

Edit1:
My settings.py looks as follows:

Edit1:
我的 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 + (
)

Edit2:

编辑2:

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

Edit3:
Ok. This is solved for me now. The solution was a conglomerate of different things. One of the problems was the missing rights. The user that executes Django, could not list the contents of the templates/ directory. chmod o+x templates/did the job. Then there were some settings in the settings.py, that changed the place to look for the templates from templates/iecl_dev/ to iecl_dev/templates/. I saw this wrong path in the error message in my web browser. But simply reverting the settings.py to the old version, was not enough. Some service(s) needed to be restarted. I simply rebooted the machine and everything was fine. Miraculously, the /admin/ page now looks nice as well.

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

Many thanks for all your tips.

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

回答by Agate

From what you've shown, I think you may have a problem with your project layout.

从您所展示的情况来看,我认为您的项目布局可能有问题。

Usually, it is set as follows :

通常,它设置如下:

├── 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

As you can see, each app inside your project can have it's own templatesdirectory.yourprojectis a bit particuliar, because it also stores unique files, such as settings.pyand wsgi.py. However, you can consider and use it as an app.

如您所见,项目中的每个应用程序都可以拥有自己的templates目录。yourproject有点特殊,因为它还存储了独特的文件,例如settings.pywsgi.py。但是,您可以考虑将其用作应用程序。

Now, if you want to use a template stored in yourproject/templates, you'll have to add yourprojectto your INSTALLED_APPS?settings.

现在,如果您想使用存储在 中的模板yourproject/templates,您必须添加yourproject到您的INSTALLED_APPS?settings 中。

Does it helps ? If not, can you edit your original post with your project layout ?

它有帮助吗?如果没有,你能用你的项目布局编辑你的原始帖子吗?

回答by Ishan070692

The reason might be that django is trying to look for your templates directory in the directory with your setting.py file Whereas actually the templates directory is in the parent directory. Try doing something like this in your settings.py

原因可能是 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,
)

The error usually is due to incorrectly configured templates directory

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

回答by himanshuxd

I wasted a lot of time trying to figure out what was wrong with my 'templates'directory and figured out :

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

You may have forgotten to add TEMPLATE_DIRin the following segment of settings.py:

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

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',
            ],
        },
    },
]

So it should have 'DIRS': [TEMPLATE_DIR,],instead of 'DIRS': [],

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