I have tried to get this to work a million times. I have lef it alone for a week and come back. I have Googled and read every post pertaining to this. I have let insecure morons belittle in messages groups without ever finding the answer. I just want this to work. When I am following along in part three of the the Django tutorial, I get to the part where you make the template for the index page, and when I go to check it in the browser, it comes up with TemplateDoesNotExist at /polls/
. I have checked file ownership, moved it all around, always changing the TEMPLATES_DIR
in setting.py
. I have pipe'd the contents of the file to cat to make sure it works. I have scarificed animals to strange Gods to get this to work. I turn to you now. I am sure it is the stupidest thing ever, I have very little doubt about this. I just want it to work.
I am not sure what parts of the code/traceback you want/need here, let me know I will post them. I am doing this on Ubuntu 10.10
EDIT
From settings.py
:
TEMPLATE_DIRS = (
"home/kevin/first/tutorial/temps"
)
This used to live in ~, but I moved into the project folder thinking it would help.
Structure(leaving out all complied python files):
~/first/tutorial/:
__init__.py,开发者_如何学编程
manage.py,
polls,
settings.py,
temps,
tut.db,
urls.py
temps:
index.html
polls:
admin.py,
__init__.py,
models.py,
tests.py,
views.py,
>>> TEMPLATE_DIRS = ( "home/kevin/first/tutorial/temps" )
>>> print TEMPLATE_DIRS
home/kevin/first/tutorial/temps
>>> type(TEMPLATE_DIRS)
<type 'str'>
This is a string, not a tuple.
TEMPLATE_DIRS = ( "home/kevin/first/tutorial/temps", )
That is a tuple. A little bit of a Python gotcha.
Furthermore, use an absolute path rather than a relative path.
First off, and this might not be relevant to your problem, but it's good practice to not use absolute paths in your files. I always have the following in my settings.py
:
PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates'),
)
You should always have a comma at the end of an element in your tuple, even if it is the only one or the last one, so that Python actually considers it to be a tuple and not evaluate to any other type.
You should also make sure that your TEMPLATE_LOADERS
setting contains or looks like this:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
If that doesn't fix it (you should still keep the above in regardless), the problem is most likely related to the template you're rendering to. Confirm that the string of the template you're using in the view corresponds correctly to the relative path of the template within your templates
directory. In other words, if the template string is 'polls/index.html'
in your view, ensure that the file is actually located at templates/polls/index.html
.
What I've used in my settings.py
configuration file was:
"TEMPLATE_DIRS = (
'/path/to/folder/Python-templates',
)"
And it worked. What I guess (and correct me if I'm wrong) is that the example in the django tutorial is making reference to the exact location of the template within the templates folder, which is polls
. Thus, in the example found in this tutorial, the polls/index.html
path to file is saying "the index.html
file in the polls
folder that is already in the template folder specified in settings.py
".
This happened to me because I skipped to part three (I wanted to learn about templates) and I forgot to add add the PollsConfig to INSTALLED_APPS
Your mysite/settings.py
should look this this:
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
After I did this change, the templates were all found.
This is a bit confusing because you don't have to do this to make your views work. But templates definitely won't load if your app's config is not in INSTALLED_APPS
.
Some things to check:
Do you use an absolute path to specify the template directory in settings.py?
Did you put the template directly into the TEMPLATE_DIR directory, or did you make a polls subfolder to put the template in?
I had the same issue as you described, doing the third part of the Django tutorial throws TemplateDoesNotExist.
Restarting the server solved it for me (did not change any configurations). Hope that helps...
精彩评论