Reputation: 30111
I'm doing the Django tutorial here: http://docs.djangoproject.com/en/1.2/intro/tutorial03/
My TEMPLATE_DIRS in the settings.py looks like this:
TEMPLATE_DIRS = (
"/webapp2/templates/"
"/webapp2/templates/polls"
# 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.
)
My urls.py looks like this:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/$', 'polls.views.index'),
(r'^polls/(?P<poll_id>\d+)/$', 'polls.views.detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'polls.views.results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
(r'^admin/', include(admin.site.urls)),
)
My views.py looks like this:
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('c:/webapp2/templates/polls/index.html')
c = Context({
'latest_poll_list': latest_poll_list,
})
return HttpResponse(t.render(c))
I think I am getting the path of my template wrong because when I simplify the views.py code to something like this, I am able to load the page.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
My index template file is located at C:/webapp2/templates/polls/index.html. What am I doing wrong?
This is the error I am getting:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 280, in run
self.result = application(self.environ, self.start_response)
File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 674, in __call__
return self.application(environ, start_response)
File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 241, in __call__
response = self.get_response(request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 141, in get_response
return self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 180, in handle_uncaught_exception
return callback(request, **param_dict)
File "C:\Python27\lib\site-packages\django\utils\decorators.py", line 76, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\defaults.py", line 30, in server_error
t = loader.get_template(template_name) # You need to create a 500.html template.
File "C:\Python27\lib\site-packages\django\template\loader.py", line 157, in get_template
template, origin = find_template(template_name)
File "C:\Python27\lib\site-packages\django\template\loader.py", line 138, in find_template
raise TemplateDoesNotExist(name)
TemplateDoesNotExist: 500.html
Upvotes: 1
Views: 3384
Reputation: 70148
TEMPLATE_DIRS = (
"/webapp2/templates/"
"/webapp2/templates/polls"
)
Python concatenates those strings during compilation if you don't put a comma in between.
And another thing: Usually you don't need to hardcode template directories. The following should be enough for most cases:
def fromRelativePath(*relativeComponents):
return os.path.join(os.path.dirname(__file__), *relativeComponents).replace("\\","/")
TEMPLATE_LOADERS = (
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
)
TEMPLATE_DIRS = (
fromRelativePath("templates"),
)
This will make Django search in "yoursite/templates" and in the "templates" directory of each intalled app.
Upvotes: 3
Reputation: 7587
Try to load templates from related path, not from absolute. In your case try this:
t = loader.get_template('index.html')
Upvotes: 2