2

I'm having a bit of difficulty understanding the way Django looks for template files.

I'm following a tutorial, and in (project directory)/thirdauth/settings.py, I have the following:

# See: http://stackoverflow.com/questions/15411164/django-templates-folders
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = [
    os.path.join(PROJECT_ROOT, 'thirdauth_app/templates'),
]

In (project root)/thirdauth/urls.py, I have the following:

urlpatterns = [
    url(r'^$', include('thirdauth_app.urls')),
    url(r'^admin/', admin.site.urls),
]

Then in (project root)/thirdauth_app/urls.py I have this:

from . import views  # <- why?

urlpatterns = [
    url(r'^$', views.home, name='home')
]

(Though as my comment indicates, I'm not exactly sure why I have to import views explicitly in an app when this isn't necessary in (project root)/thirdauth/urls.py, but I suppose that's a separate question.)

In (project_root)/thirdauth_app/views.py is the following:

def home(request):
    context = RequestContext(request, {'user': request.user})
    return render_to_response('thirdauth_app/home.html', context_instance=context)

Finally, the template file itself is located at (project root)/thirdauth_app/templates/thirdauth_app/home.html

Unfortunately, when running the server, I'm still getting the TemplateDoesNotExist error (TemplateDoesNotExist at /)

Obviously I've misunderstood something crucial, so any advice would be hugely appreciated.

Edit: Per request, here is the full traceback:

Environment:


Request Method: GET
Request URL: http://localhost:8000/

Django Version: 1.9.1
Python Version: 2.7.6
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'thirdauth']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 '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']

Template loader postmortem
Django tried loading these templates, in this order:

Using engine :
    * django.template.loaders.app_directories.Loader: /home/dbc/venv_projects/thirdauth/local/lib/python2.7/site-packages/django/contrib/admin/templates/home.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /home/dbc/venv_projects/thirdauth/local/lib/python2.7/site-packages/django/contrib/auth/templates/home.html (Source does not exist)



Traceback:

File "/home/dbc/venv_projects/thirdauth/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/dbc/venv_projects/thirdauth/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/dbc/venv_projects/thirdauth/thirdauth/thirdauth_app/views.py" in home
  7.     return render_to_response('home.html', context_instance=context)

File "/home/dbc/venv_projects/thirdauth/local/lib/python2.7/site-packages/django/shortcuts.py" in render_to_response
  45.             using=using)

File "/home/dbc/venv_projects/thirdauth/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  137.             raise TemplateDoesNotExist(template_name, chain=chain)

Exception Type: TemplateDoesNotExist at /
Exception Value: home.html
dcgenjin
  • 1,108
  • 3
  • 12
  • 25
  • `from . import views <- why?` Because your `views.py` present on the directy where `urls.py` file exists. – Avinash Raj Jan 03 '16 at 01:11
  • 1
    Are you using the new-style [`TEMPLATES`](https://docs.djangoproject.com/en/1.9/ref/settings/#templates) setting? If you are, Django will ignore `TEMPLATE_DIRS`. You should set the `DIRS` option inside `TEMPLATES` instead. – knbk Jan 03 '16 at 01:48

1 Answers1

2

change your setting file like this :

settings.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#...
#...
#...
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',
                'django.core.context_processors.static',
            ],
        },
    },
]

in this line you can specify any folder for templates, it's 'templates' by default

'DIRS': [os.path.join(BASE_DIR,'<<<folder name>>>')],

you can build paths inside the project like this: os.path.join(BASE_DIR, ...)

also be sure that define your app to project, add this code at the end of INSTALLED_APPS section in settings.py :

'thirdauth_app',

(Though as my comment indicates, I'm not exactly sure why I have to import views explicitly in an app when this isn't necessary in (project root)/thirdauth/urls.py, but I suppose that's a separate question.)

if see your code, you are using a view ( views.home )

url(r'^$', views.home, name='home')

so you need to import your views to urls.py

then in your views.py :

from django.shortcuts import render

def home(request):
    return render(request,'thirdauth_app/home.html')
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59