Error on Tutorial Part 3 - https://docs.djangoproject.com/en/1.3/intro/tutorial03/

118 views
Skip to first unread message

Django_for_SB

unread,
Feb 29, 2012, 2:46:11 PM2/29/12
to Django users
Hello All,

I'm going through the tutorial on djangoproject.com, and can't seem to
hurdle over this section that reads "Write views that actually do
something"

Here's the code I have so far, which is directly copied from the
tutorial or prescribed by the tutorial:

views.py:
"from django.template import Context, loader
from polls.models import Poll
from django.http import HttpResponse

def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
t = loader.get_template('polls/index.html')
c = Context({
'latest_poll_list': latest_poll_list,
})
return HttpResponse(t.render(c))"



settings.py:
"...
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/
django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'C:/Python27/my_Djando_projects/mysite/My_Templates/admin/
base_site.html'
'C:/Python27/my_Djando_projects/mysite/My_Templates/admin/
index.html'
'C:/Python27/my_Djando_projects/mysite/My_Templates/polls/
index.html'
)
..."


index.html:
"{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></
li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
"




I keep getting the same error, which reads:


TemplateDoesNotExist at /polls/

polls/index.html

Request Method: GET
Request URL: http://localhost:8000/polls/
Django Version: 1.3.1
Exception Type: TemplateDoesNotExist
Exception Value:

polls/index.html

Exception Location: C:\Python27\lib\site-packages\django\template
\loader.py in find_template, line 138
Python Executable: C:\Python27\python.exe
Python Version: 2.7.2
Python Path:

['C:\\Python27\\my_Djando_projects\\mysite',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']

Server time: Wed, 29 Feb 2012 11:32:54 -0800
Template-loader postmortem

Django tried loading these templates, in this order:

Using loader django.template.loaders.filesystem.Loader:
c:\python27\my_djando_projects\mysite\my_templates\admin
\base_site.html
c:\python27\my_djando_projects\mysite\my_templates\admin\index.html
c:\python27\my_djando_projects\mysite\my_templates\polls\index.html
\polls\index.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
c:\python27\lib\site-packages\django\contrib\admin\templates\polls
\index.html (File does not exist)




What on earth am I doing wrong here? I've so many different variations
of my settings.py, views.py, and index.html. Any help would be much
appreciated.


Thanks,

SB

Anoop Thomas Mathew

unread,
Feb 29, 2012, 2:48:53 PM2/29/12
to django...@googlegroups.com
You have to give template directories, not template names in the settings.py.
Thanks,
Anoop
atm
___
Life is short, Live it hard.





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Sami Balbaky

unread,
Feb 29, 2012, 3:03:21 PM2/29/12
to django...@googlegroups.com
Hi Anoop,

Thank you for the kind reply, I've tried that already. Here are the 3 variations that I've attempted so far within settings.py in TEMPLATE_DIRS:

'C:/Python27/my_Djando_projects/mysite/My_Templates/polls/index.html'
'C:/Python27/my_Djando_projects/mysite/My_Templates/polls/'
'C:/Python27/my_Djando_projects/mysite/My_Templates/'


They all yield the same error unfortunately. I'm coding this on a Windows Vista system, fyi.

Best,

SB
--
Sami Balbaky
System Engineer - Ultrawave Labs

Sandro Dutra

unread,
Feb 29, 2012, 3:19:03 PM2/29/12
to django...@googlegroups.com
Try:
'C:\Python27\my_Djando_projects\mysite\My_Templates\polls\index.html'
'C:\Python27\my_Djando_projects\mysite\My_Templates\polls\'
'C:\Python27\my_Djando_projects\mysite\My_Templates\'

Or:
abspath = lambda *p: os.path.abspath(os.path.join(*p))
PROJECT_ROOT = abspath(os.path.dirname(__file__))
(...)
TEMPLATE_DIRS = (abspath(PROJECT_ROOT, "My_Templates"),)




2012/2/29 Sami Balbaky <sami.b...@gmail.com>

Daniel Roseman

unread,
Feb 29, 2012, 5:00:50 PM2/29/12
to django...@googlegroups.com
On Wednesday, 29 February 2012 20:03:21 UTC, Django_for_SB wrote:
Hi Anoop,

Thank you for the kind reply, I've tried that already. Here are the 3 variations that I've attempted so far within settings.py in TEMPLATE_DIRS:

'C:/Python27/my_Djando_projects/mysite/My_Templates/polls/index.html'
'C:/Python27/my_Djando_projects/mysite/My_Templates/polls/'
'C:/Python27/my_Djando_projects/mysite/My_Templates/'


They all yield the same error unfortunately. I'm coding this on a Windows Vista system, fyi.

Best,

SB

On Wed, Feb 29, 2012 at 11:48 AM, Anoop Thomas Mathew wrote:
You have to give template directories, not template names in the settings.py.
Thanks,
Anoop
atm
___
Life is short, Live it hard.


You also need to separate the different strings with commas. Otherwise Python will automatically concatenate them, as they are within parentheses.
--
DR. 
Message has been deleted

Python_Junkie

unread,
Mar 1, 2012, 6:42:20 PM3/1/12
to django...@googlegroups.com
Just to get you past the error and display your data, I suggest hard coding the directory path in your view.
The work your way backwards to the settings file


try 

t = loader.get_template('C:\\Python27\\my_Djando_projects\\mysite\\My_Templates\\polls\\index.html'') 


You could also add a little bit of python into your view to list the directory and print out the variable in the template just for a sanity check

path=C:\\Python27\\my_Djando_projects\\mysite\\My_Templates\\polls\\'
var=os.lisdir(path)

 c = Context({
       'latest_poll_list': latest_poll_list,"var":var,
   })

and then add the dictionary value {{var}} in your template
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.

Python_Junkie

unread,
Mar 1, 2012, 6:42:38 PM3/1/12
to django...@googlegroups.com

Sami Balbaky

unread,
Mar 2, 2012, 4:47:54 PM3/2/12
to django...@googlegroups.com
Thank you everyone for your very insightful and helps remarks. I had 2 issues with my code:

1) I didn't properly separate my directory strings with commas and
2) I need to code the directory structure, and the absolute path to the file itself.

Here is the correct code from within settings.py:

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".

    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    'C:/Python27/my_Djando_
projects/mysite/My_Templates/admin/base_site.html',
    'C:/Python27/my_Djando_projects/mysite/My_Templates/admin/index.html',
    'C:/Python27/my_Djando_projects/mysite/My_Templates/',
)





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/klXbEFxYWH4J.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Daniel Roseman

unread,
Mar 2, 2012, 5:21:31 PM3/2/12
to django...@googlegroups.com
On Friday, 2 March 2012 21:47:54 UTC, Django_for_SB wrote:
Thank you everyone for your very insightful and helps remarks. I had 2 issues with my code:

1) I didn't properly separate my directory strings with commas and
2) I need to code the directory structure, and the absolute path to the file itself.

Here is the correct code from within settings.py:

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    'C:/Python27/my_Djando_
projects/mysite/My_Templates/admin/base_site.html',
    'C:/Python27/my_Djando_projects/mysite/My_Templates/admin/index.html',
    'C:/Python27/my_Djando_projects/mysite/My_Templates/',
)

No, this is *not* correct. It may work, but that's because the first two will simply not be found.

You *only* need the last line here (including the comma).
--
DR. 
 

Sami Balbaky

unread,
Mar 2, 2012, 6:05:36 PM3/2/12
to django...@googlegroups.com
Thank you for the clarification DR. I finished the beginning tutorial on djangoproject.com. I'm certainly going to need a lot more practice. I'll fix my code with your changes.

Best,

SB

 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/xcPMN9h-SIgJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

sillyou su

unread,
Mar 2, 2012, 9:13:05 PM3/2/12
to Django users
like this:
your_project---
|---__init__.py
|---settings.py
|---urls.py
|---manage.py
|---app_A---
|
|---__init__.py
|---models.py
|---view.py
|---templates---
|
|---index.html
Reply all
Reply to author
Forward
0 new messages