简体   繁体   中英

Django Can't Find App Templates

I'm working through the official django 1.7 tutorial found here . Everything is going smoothly except django can't find the templates for apps. It finds templates that I put under workspace/mysite/templates but not under workspace/mysite/polls/templates.

workspace is a folder I have in my home directory where I'm keeping all my web projects.

My path is ~workspace/mysite/

and the project structure is

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

I'll just list the contents of each folder for brevity:

  • db.sqlite3 - is a file
  • manage.py - is a file
  • mysite - is the project folder
  • mysite_env - is the virtualenv folder
  • polls - is the app folder and contains a file directory structure with the templates that aren't being picked up. The structure within the open directory is templates/polls/index.html
  • templates - is the project template dir. It is being picked up by django

in my mysite/settings.py I have specified

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

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

polls/views.py settings for index is:

`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))`

the actual polls/templates/polls/index.html contains:

`{% 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 regex for matching index for /polls/:

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

The specific error I'm getting 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`

The traceback it's giving is:

`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've been trying to figure out what's wrong for way too long now. Any ideas based on currently accepted practices are welcome.

Thank you.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM