Reputation: 13
I'm using python 3.5.2 and django 1.11.6. My OS is win10. I cannot load my template in the installed_app path.
I've created a file at "polls/template/polls/index.html". I've also add the app, polls, into INSTALLED_APPS and set APP_DIRS in TEMPLATES to True. I cannot figure out why django won't load the template in the INSTALLED_APPS template folder as the tutorial told me.
I used the following code snippet to load the template in my view.
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {
'latest_question_list': latest_question_list,
}
return HttpResponse(template.render(context, request))
I got this TemplateDoesNotExist. I'm wondering that why django doesn't search the path, "polls/template". The paths searched by django are as following.
My INSTALLED_APPS is as following.
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
My TEMPLATES setting is as following.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
Upvotes: 1
Views: 2958
Reputation: 22270
I arrived there having the same issue.
In my case, it was not that I made a typo template
instead of templates
, but for some reasons, my app was not registered in:
INSTALLED_APPS = [
'appname.apps.AppnameConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
}
in setting.py
.
Hope this can help someone!
Upvotes: 0
Reputation:
Another way is, you can set template path in TEMPLATES in DIRS in settings.py.
'DIRS': [str(ROOT_DIR.path('polls/templates/polls/')),],
and set the ROOT_DIR which is root directory of your project, you can use environ to set root directory as:
ROOT_DIR = environ.Path(__file__) - 2
As this is complex compare to direct add polls/templates/polls/index.html
but this will be helpful in general case. When project is going in high phase than it is important to convert that in general way.
Upvotes: 0
Reputation: 31514
Your template directory is wrong. It should be polls/templates/polls/index.html
- templates instead of template. From the tutorial:
First, create a directory called templates in your polls directory. Django will look for templates in there.
Within the templates directory you have just created, create another directory called polls, and within that create a file called index.html. In other words, your template should be at polls/templates/polls/index.html.
Upvotes: 1