Reputation: 503
I am trying to create a home page for my django project. I keep getting this error:
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 looks like this:
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',
],
},
},
]
My templates folder is located in my project folder and contains a home.html
This is the code from loader.py (but I don't have my head around what it's doing / requesting exactly):
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)
Everything appears to be right, I'm not sure what I'm overlooking. Any ideas?
UPDATE: 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)
Upvotes: 0
Views: 2113
Reputation: 1
write your 'templates' name correct in setting.py insatalledApp , DIRS['templates'] and restart server
Upvotes: 0
Reputation: 503
It was a combination of the things suggested - just a matter of getting the right sequence of things I guess.
I added the directory the home.html was located in:(in views.py)
def home(request):
return render(request, 'epicar/home.html')
Then I moved the templates folder BACK into the main project directory:
sudo service apache2 restart
and wala!
no errors. :)
Upvotes: 2
Reputation: 2212
According to your view and settings file, home.html
have to be in templates
folder.
So change the path of the home.html
in the view, from:
def home(request):
return render(request, ('home.html'))
to
def home(request):
return render(request, ('pathtoapp/home.html'))
or move home.html
in the root of templates
Upvotes: 1
Reputation: 793
Usually when I get this error I've forgotten to add the app the the INSTALLED_APPS
setting.
The default loader looks in TEMPLATES['DIRS']
and then a templates folder in the root of each app specified in INSTALLED_APPS
Upvotes: 2