Django cannot find application templates

I am working on the official django 1.7 tutorial found here . Everything goes smoothly, except that django cannot find application templates. It finds templates that I put in workspace / mysite / templates, but not under workspace / mysite / polls / templates.

workspace is the folder that I have in my home directory, where I store all my web projects.

My path ~ workspace / mysite /

and project structure

`workspace | mysite | db.sqlite3 - manage.py - mysite - mysite_env - polls - templates` 

I will simply list the contents of each folder for short:

  • db.sqlite3 is a file
  • manage.py is a file
  • mysite is the project folder
  • mysite_env is the virtualenv folder
  • polls is the application folder and contains the structure of the file directory with templates that are not collected. The structure inside the open directory is templates / polls / index.html
  • templates is a project template. He is picked up by django

in my mysite / settings.py I specified

 `TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]` 

I tried to add the path to the polls / templates folder, but that didn't work either.

Settings

polls / views.py for the index:

 `def index(request): latest_question_list = Question.objects.order_by('-pub_date')[:5] template = loader.get_template('/polls/index.html') context = RequestContext(request, { 'latest_question_list': latest_question_list, }) return HttpResponse(template.render(context))` 

actual polls / templates / polls / index.html contain:

 `{% if latest_question_list % } <ul> {% for question in latest_question_list %} <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a>a></li> {% endfor %} </ul> {% else %} <p>No Polls are available.</p> {% endif %}` 

and last but not least: polls / urls.py contains this regular expression to match the index for / polls /:

 `#ex: /polls/ url(r'^$', views.index, name='index'),` 

The specific error I get is:

 `TemplateDoesNotExist at /polls/ /polls/index.html Request Method: GET Request URL: http://127.0.0.1:8000/polls/ Django Version: 1.7.4 Exception Type: TemplateDoesNotExist Exception Value: /polls/index.html Exception Location: /home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages/django/template/loader.py in find_template, line 136 Python Executable: /home/jeremiah/workspace/mysite/mysite_env/bin/python Python Version: 3.4.0 Python Path: ['/home/jeremiah/workspace/mysite', '/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4', '/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/plat-x86_64-linux-gnu', '/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/lib-dynload', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages'] Server time: Wed, 4 Feb 2015 18:27:56 -0800` 

Tracked traffic:

 `nvironment: Request Method: GET Request URL: http://127.0.0.1:8000/polls/ Django Version: 1.7.4 Python Version: 3.4.0 Installed Applications: ('django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'polls') Installed Middleware: ('django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware') Template Loader Error: Django tried loading these templates, in this order: Using loader django.template.loaders.filesystem.Loader: Using loader django.template.loaders.app_directories.Loader: Traceback: File "/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response 111. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/jeremiah/workspace/mysite/polls/views.py" in index 9. template = loader.get_template('/polls/index.html') File "/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages/django/template/loader.py" in get_template 144. template, origin = find_template(template_name, dirs) File "/home/jeremiah/workspace/mysite/mysite_env/lib/python3.4/site-packages/django/template/loader.py" in find_template 136. raise TemplateDoesNotExist(name) Exception Type: TemplateDoesNotExist at /polls/ Exception Value: /polls/index.html` 

Can someone please help me with this? I tried to figure out what was wrong for too long. Any ideas based on current practice are welcome.

Thanks.

+3
source share
1 answer

Remove the first line / from the template name. It should be:

 template = loader.get_template('polls/index.html') 
+5
source

All Articles