模板不存在/错误异常at- loader.py第43行

时间:2016-12-08 14:07:16

标签: python django python-2.7 django-templates

我正在尝试为我的django项目创建一个主页。我一直收到这个错误:

TemplateDoesNotExist at /
home.html
Request Method: GET
Request URL:    http://xtradev.local/
Django Version: 1.9
Exception Type: TemplateDoesNotExist
Exception Value:    
home.html
Exception Location: /home/epic/EPIC/venv/lib/python2.7/site-packages/django/template/loader.py in get_template, line 43
Python Executable:  /usr/bin/python
Python Version: 2.7.9
Python Path:    
['/home/epic/EPIC/EPIC-PROJECT/EPIC-Django/EPIC_AR',
 '/home/epic/EPIC/venv/lib/python2.7/site-packages',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/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/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gst-0.10',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7']
Server time:    Thu, 8 Dec 2016 14:04:41 +0000 

我的settings.py看起来像这样:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    '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',
        ],
    },
},

我的模板文件夹位于我的项目文件夹中,包含一个home.html

这是来自loader.py的代码(但我不知道它正在做什么/正在请求的内容):

def get_template(template_name, dirs=_dirs_undefined, using=None):
"""
Loads and returns a template for the given name.

Raises TemplateDoesNotExist if no such template exists.
"""
chain = []
engines = _engine_list(using)
for engine in engines:
    try:
        # This is required for deprecating the dirs argument. Simply
        # return engine.get_template(template_name) in Django 1.10.
        if isinstance(engine, DjangoTemplates):
            return engine.get_template(template_name, dirs)
        elif dirs is not _dirs_undefined:
            warnings.warn(
                "Skipping template backend %s because its get_template "
                "method doesn't support the dirs argument." % engine.name,
                stacklevel=2)
        else:
            return engine.get_template(template_name)
    except TemplateDoesNotExist as e:
        chain.append(e)

raise TemplateDoesNotExist(template_name, chain=chain)

一切似乎都是正确的,我不确定我在忽视什么。有什么想法吗?

更新: VIEWS.PY

from django.contrib.auth.models import User, Group
from django.shortcuts import render, HttpResponse
from rest_framework import filters
from rest_framework import viewsets

from serializers import UserSerializer, GroupSerializer

def home(request):
    return render(request, ('home.html'))

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

URLS.PY

admin.site.site_header = 'EPIC.AR Administration'

urlpatterns = [
    url(r'^$', 'EPIC_AR.views.home', name='Home'),
    url(r'^admin/', admin.site.urls),
    url(r'^api/1.0/', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),

]


Template-loader postmortem

Django tried loading these templates, in this order:

Using engine django:
django.template.loaders.filesystem.Loader: /home/epic/EPIC/EPIC-PROJECT/EPIC-Django/EPIC_AR/templates/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/epic/EPIC/venv/lib/python2.7/site-packages/suit/templates/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/epic/EPIC/venv/lib/python2.7/site-packages/django/contrib/admin/templates/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/epic/EPIC/venv/lib/python2.7/site-packages/django/contrib/auth/templates/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/epic/EPIC/venv/lib/python2.7/site-packages/rest_framework/templates/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/epic/EPIC/venv/lib/python2.7/site-packages/crispy_forms/templates/home.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/epic/EPIC/EPIC-PROJECT/EPIC-Django/EPIC_AR/home/templates/home.html (Source does not exist)

enter image description here

4 个答案:

答案 0 :(得分:2)

通常当我收到此错误时,我忘记将应用添加到INSTALLED_APPS设置。

默认加载器在TEMPLATES['DIRS']中查找,然后在INSTALLED_APPS

中指定的每个应用的根目录中找到模板文件夹

答案 1 :(得分:1)

根据您的观看和设置文件,home.html必须位于templates文件夹中。

因此,请改变视图中home.html的路径:

def home(request):
    return render(request, ('home.html'))

def home(request):
    return render(request, ('pathtoapp/home.html'))

或在home.html

的根目录中移动templates

答案 2 :(得分:1)

这是所建议的事情的组合 - 只是我想要正确的事情序列。

我添加了home.html所在的目录:(在views.py中)

def home(request):
return render(request, 'epicar/home.html')

然后我将模板文件夹BACK移动到主项目目录中:

enter image description here

sudo service apache2 restart

和wala!

enter image description here

没有错误。 :)

答案 3 :(得分:0)

在setting.py insatalledApp,DIRS ['templates']中正确输入您的“模板”名称并重新启动服务器