Python Django:TemplateDoesNotExist at/home.html
我正在尝试使用Django和Udemy教程()创建第一个站点,我坚持学习第7课:主视图。运行服务器后,我收到错误:Python Django:TemplateDoesNotExist at/home.html,python,django,Python,Django,我正在尝试使用Django和Udemy教程()创建第一个站点,我坚持学习第7课:主视图。运行服务器后,我收到错误: Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 1.8.2 Python Version: 3.4.2 Installed Applications: ('django.contrib.admin', 'django.contrib.auth', 'django.contrib.con
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.8.2
Python Version: 3.4.2
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'profiles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/root/Documents/tryDjango/lib/python3.4/site-packages/django/contrib/ admin/templates/home.html (File does not exist)
/root/Documents/tryDjango/lib/python3.4/site-packages/django/contrib/auth/templates/home.html (File does not exist)
Traceback:
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/core/ handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/root/Documents/tryDjango/src/profiles/views.py" in home
7. return render(request, template, context)
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/shortcuts.py" in render
67. template_name, context, request=request, using=using)
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/template/loader.py" in render_to_string
98. template = get_template(template_name, using=using)
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/template/loader.py" in get_template
46. raise TemplateDoesNotExist(template_name)
Exception Type: TemplateDoesNotExist at /
Exception Value: home.html
我创建了应用程序配置文件,我的结构树如下所示:
/tryDjango
/src
/tryDjango
urls.py
setting.py
_init_.py
wsgi.py
/profiles
admin.py
models.py
_init_.py
tests.py
views.py
/static
/templates
home.html
设置.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = XXXX
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'profiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'tryDjango.urls'
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',
],
},
},
]
WSGI_APPLICATION = 'tryDjango.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
TEMPLATE_DIRS = (os.path.join(os.path.dirname(BASE_DIR), 'static', 'templates'), )
url.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'profiles.views.home', name='home'),
]
views.py
from django.shortcuts import render
# Create your views here.
def home(request):
context = locals()
template = 'home.html'
return render(request, template, context)`
我还尝试了其他命令,以下是输出:
>>>from django.conf import settings
>>>print (settings.TEMPLATE_DIRS)
('/root/Documents/tryDjango/static/templates',)
>>> from django.template import loader
>>> print(loader.get_template('home.html'))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/ template/loader.py", line 46, in get_template
raise TemplateDoesNotExist(template_name)
django.template.base.TemplateDoesNotExist: home.html
来自django.conf导入设置的>
>>>打印(设置.模板\u目录)
('/root/Documents/tryDjango/static/templates',)
>>>从django.template导入加载器
>>>打印(loader.get_模板('home.html'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
get_模板中的文件“/root/Documents/tryDjango/lib/python3.4/site-packages/django/template/loader.py”,第46行
raise TEMPLATEDOESNOTEXTIST(模板名称)
django.template.base.TemplateDoesNotExist:home.html
非常感谢!
P.S我使用Debian。
将模板放在
静态文件中不是一个好主意,因为那里的文件将随时可以在internet上使用,而且您可能不想将原始后端暴露给世界;)代码>
尝试此文件结构(请注意,我已移动了模板
目录):
Django在每个已安装的应用程序的目录中搜索,并使用匹配的templates
文件夹中的第一个模板
如果您依赖于此(我建议),则需要将'tryDjango',
添加到您的已安装的应用程序中
或者,将查找您为模板定义的路径
为此,您需要将完整路径添加到或。您使用的是Django 1.7模板设置(template_DIRS)和1.8中的新样式(TEMPLATES)的混合。选择一种风格并坚持它。可能重复
/tryDjango
/src
/tryDjango
urls.py
setting.py
_init_.py
wsgi.py
/templates
home.html
/profiles
admin.py
models.py
_init_.py
tests.py
views.py