"TemplateDoesNotExist at /"

883 views
Skip to first unread message

Richard Belew

unread,
Feb 25, 2017, 4:33:32 PM2/25/17
to Django users
"TemplateDoesNotExist at /"

(full trace log at http://dpaste.com/3XZ8H3C)

this must be near the top of django newby issues, but i'm stumped
on the simplest example i can generate:

i've used the `settings.TEMPLATES.DIRS` variable to specify a
shared templates directory (also to simplify things), and
am using an application's `views.py` file to anchor a
named url, but the django engine still cannot find the `index.html` file
i've put there.

what am i missing?  any hints appreciated,  Rik

.
├── db.sqlite3
├── djggApp
  ├── __init__.py
  ├── __init__.pyc
...
  ├── views.py
  └── views.pyc
├── djggProj
  ├── __init__.py
  ├── __init__.pyc
  ├── settings.py
  ├── settings.pyc
  ├── urls.py
  ├── urls.pyc
  ├── wsgi.py
  └── wsgi.pyc
├── manage.py
└── templates
...
   
├── index.html
* djggProj/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',
               
],
           
},
       
},
   
]


* djggProj/urls.py

  
  from djggApp import views
   
    urlpatterns
= [
        url
(r'^$', views.index, name='index'),
   
...
        url
(r'^admin/', admin.site.urls),
   
]


* djggApp/views.py

   def index(request):
       
template = loader.get_template('index.html')
        context
= Context({})
       
return HttpResponse(template.render(context))




Richard Belew

unread,
Feb 25, 2017, 5:05:21 PM2/25/17
to Django users
another possibly relevant bit:  the Template-loader postmortem shows:

Django tried loading these templates, in this order:

Using engine django:

  • django.template.loaders.filesystem.Loader: /Data/virtualenv/django/djggProj/('/Data/virtualenv/django/djggProj/templates',)/index.html (Source does not exist)

this error message has a badly formed path "/Data/virtualenv/django/djggProj/('/Data/virtualenv/django/djggProj/templates',)/index.html" ; should i care?

ludovic coues

unread,
Feb 25, 2017, 5:08:21 PM2/25/17
to django...@googlegroups.com
yep, look like your settings are wrong
try to add that at the end of your settings.py and watch the console
you are using to launch django

print(TEMPLATES["DIRS"])
print(BASE_DIR)
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/8fbefe48-945d-4f4a-9629-d8e59dfd23af%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Richard Belew

unread,
Feb 25, 2017, 5:22:21 PM2/25/17
to Django users
wow, very good and thank you!    it was this mal-formed line:


 
       'DIRS': [ ( os.path.join(BASE_DIR, 'templates'), ) ],


which should have been:

        'DIRS': [ os.path.join(BASE_DIR, 'templates'),  ],

i had an extra set of parentheses, for whatever bad reason.  thanks again!

Michal Petrucha

unread,
Feb 25, 2017, 5:30:22 PM2/25/17
to Django users
On Sat, Feb 25, 2017 at 01:20:49PM -0800, Richard Belew wrote:
> "TemplateDoesNotExist at /"
>
> (full trace log at http://dpaste.com/3XZ8H3C)
>
> this must be near the top of django newby issues, but i'm stumped
> on the simplest example i can generate:
>
> i've used the `settings.TEMPLATES.DIRS` variable to specify a
> shared templates directory (also to simplify things), and
> am using an application's `views.py` file to anchor a
> named url, but the django engine still cannot find the `index.html` file
> i've put there.
>
> what am i missing? any hints appreciated, Rik

[...]

> TEMPLATES = [
> {
> 'BACKEND': 'django.template.backends.django.DjangoTemplates',
>
> 'DIRS': [ ( os.path.join(BASE_DIR, 'templates'), ) ],

This appears to be the incorrect line – here, you set 'DIRS' to a list
containing a single item, which is a tuple containing a path. The
problem is that you wrap the path twice. Django only expects a simple
list of paths, so if you just drop the tuple wrapping the path, you
should be fine::

'DIRS': [os.path.join(BASE_DIR, 'templates')],

> * djggApp/views.py
>
> def index(request):
> template = loader.get_template('index.html')
> context = Context({})
> return HttpResponse(template.render(context))

This is not strictly related to your problem, but I would strongly
advise not to render templates used as HTTP responses in this way.
Instead, you should be using either the render shortcut [1], or
TemplateResponse [2]. They are mostly equivalent, and both accomplish
the same thing as the above code, only with a single function call.
More often than not, less code is better.

Also, unlike TemplateResponse and render, the code above will not
apply context processors, unless you change that Context to a
RequestContext, which is something that's super easy to forget, and
I've actually seen people get bitten by that in the past, and spend a
lot of time trying to figure out why their templates are not behaving.

Cheers,

Michal

[1]: https://docs.djangoproject.com/en/1.10/topics/http/shortcuts/#render
[2]: https://docs.djangoproject.com/en/1.10/ref/template-response/#templateresponse-objects
signature.asc
Reply all
Reply to author
Forward
0 new messages