Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 模板在-loader.py第43行不存在/错误异常_Python_Django_Python 2.7_Django Templates - Fatal编程技术网

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

Python 模板在-loader.py第43行不存在/错误异常,python,django,python-2.7,django-templates,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/EP

我正在尝试为我的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 
My 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中的代码(但我不知道它到底在做什么/请求什么):

一切似乎都是对的,我不确定我忽略了什么。有什么想法吗

更新: 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
url.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)

通常当我遇到此错误时,我忘记将应用添加到安装的应用设置中


默认加载程序在
TEMPLATES['DIRS']
中查找,然后在
INSTALLED\u APPS

中指定的每个应用的根目录中查找一个TEMPLATES文件夹。通常,当我遇到此错误时,我忘记将应用添加到
INSTALLED\u APPS
设置中


默认加载程序在
TEMPLATES['DIRS]
中查找,然后在
INSTALLED\u APPS

中指定的每个应用的根目录中查找模板文件夹。根据您的视图和设置文件,
home.html
必须位于
TEMPLATES
文件夹中

因此,从以下位置更改视图中
home.html
的路径:

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


或者将
home.html
移动到
templates

的根目录中,根据您的视图和设置文件,
home.html
必须位于
templates
文件夹中

因此,从以下位置更改视图中
home.html
的路径:

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


或者在
模板的根目录中移动
home.html

这是建议的事情的组合-我想只是得到正确的事情顺序的问题

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

然后,我将templates文件夹移回主项目目录:

还有瓦拉


没有错误。:)

这是所建议的事情的组合——我想只是得到正确的事情顺序的问题

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

然后,我将templates文件夹移回主项目目录:

还有瓦拉



没有错误。:)

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

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

所有应用都安装在settings.py中。“我的模板”文件夹位于主项目文件夹中。我把它放在主屏幕上,然后我把它调高一层,看看这是否有帮助。因此,它当前与项目中的所有应用处于同一级别。所有应用都安装在settings.py中。“我的模板”文件夹位于主项目文件夹中。我把它放在主屏幕上,然后我把它调高一层,看看这是否有帮助。因此,它当前与项目中的所有应用程序处于同一级别。此错误出现在您的整个项目中,还是仅出现在一个应用程序中?如果只有一个应用程序,请显示此应用程序的视图,然后再问一个问题,
home.html
存在于何处?我想我的项目-我只有一个模板,我正在尝试将其用作主页。我没有其他的。我试着把它放在一个应用程序中,但也不起作用。home.html存在于应用程序级别的主项目文件夹中-不在应用程序中,也不在与settings.py相同的文件夹中(尽管它以前处于该级别),我将其移动以尝试不同的内容。因此,显示
view.py
url.py
通常错误页面会告诉您Django试图加载模板的位置。如果是的话,那么请在你的问题中包含这个错误。这个错误在你的整个项目中都存在,还是只存在于一个应用程序中?如果只有一个应用程序,请显示此应用程序的视图,然后再问一个问题,
home.html
存在于何处?我想我的项目-我只有一个模板,我正在尝试将其用作主页。我没有其他的。我试着把它放在一个应用程序中,但也不起作用。home.html存在于应用程序级别的主项目文件夹中-不在应用程序中,也不在与settings.py相同的文件夹中(尽管它以前处于该级别),我将其移动以尝试不同的内容。因此,显示
view.py
url.py
通常错误页面会告诉您Django试图加载模板的位置。如果是的话,那么请在你的问题中包括这一点。我试过了。相同的错误,只是现在它列出了文件路径:TemplateDoesNotExist at/EPIC_AR/templates/epicar/home.html请求方法:获取请求URL:Django版本:1.9异常类型:TemplateDoesNotExist异常值:/EPIC Django/EPIC_AR/templates/epicar/home.html异常位置:/home/epic/epic/venv/lib/python2.7/site-packages/django/template/loader.py在get_template第43行很难回答您的问题,因为我看不到您的项目的完整结构和文件夹名称Nope。我试过了。相同的错误,只是现在它列出了文件路径:TemplateDoesNotExist at/EPIC_AR/templates/epicar/home.html请求方法:获取请求URL:Django版本:1.9异常类型:TemplateDoesNotExist异常值:/EPIC Django/EPIC_AR/templates/epicar/home.html异常位置:/home/epic/epic/venv/lib/python2.7/site-packages/django/template/loader.py在get_template第43行很难回答您的问题,因为我看不到项目的完整结构和文件夹名称。这还取决于您使用的django版本。对于版本3.>,您可能不需要包括'DIRS':[os.path.join(BASE_DIR,'templates')],因为Django有一种在项目中自动处理模板定义的方法。理想的做法是在返回呈现的模板之前,确保所有应用程序目录的appname目录中都有templates/appname。这还取决于您使用的Django版本。对于版本3.>,你可能不需要进去
def home(request):
    return render(request, ('pathtoapp/home.html'))
def home(request):
return render(request, 'epicar/home.html')
sudo service apache2 restart