简体   繁体   中英

Django Can't Find My Templates

I'm running Python 2.6.1 and Django 1.2.1 on Windows XP SP3. I'm using JetBrains PyCharm 1.0 to create and deploy my Django apps.

I'm relatively inexperienced with Python, and I'm starting to learn Django by following along with "Writing Your First Django App" from the web site - the poll application. I'm stuck on part 3 .

Everything is fine when I add the simple callback functions for "Writing your first view".

I hit the snag when I get to "Write views that actually do something."

I followed the instructions to modify the index view:

  1. Add a new method to views.py (Note - template is ready from 'polls/index.html'):
  2. Add index.html template to site-templates/polls/ folder
  3. Modify settings.py to point to site-templates folder

Here's the code in my views.py:

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('polls/index.html')
    c = Context({
        'latest_poll_list': latest_poll_list,
    })
    return HttpResponse(t.render(c))

Here's the line in my settings.py:

TEMPLATE_DIRS = ('/site-templates/')

But still I get this message when I run:

TemplateDoesNotExist at /polls/
polls/index.html
Request Method: GET
Request URL:    http://localhost:8000/polls/
Django Version: 1.2.1
Exception Type: TemplateDoesNotExist
Exception Value:    
polls/index.html

The exception is thrown in loader.py. My debug settings look like this:

TEMPLATE_CONTEXT_PROCESSORS 
('django.core.context_processors.auth', 'django.core.context_processors.request')
TEMPLATE_DEBUG  
True
TEMPLATE_DIRS   
('/site-templates',)
TEMPLATE_LOADERS    
('django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader')

My directory structure looks like this:

替代文字

What did I miss? Is the settings.py incorrect? Please advise.

I was faced to the same problem. The mistake in my case was, that the 'app' was not in the INSTALLED_APPS list at the project settings.py file.

The error raise an error message they suggests similar error.

line 25, in get_template TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: authControll/index.html

settings.py --> Application definition

INSTALLED_APPS = [
    ...,
    'authControll'
]

You must use absolute paths in the TEMPLATE_DIRS setting.

Convenient thing to do, at the top of your settings, insert:

import os
DIRNAME = os.path.abspath(os.path.dirname(__file__))

Then anywhere you use a path, use os.path.join . Example, your TEMPLATE_DIRS would become:

TEMPLATE_DIRS = (
    os.path.join(DIRNAME, 'site-templates/'),
)

Django has a sort of patterns and philosophy. Try to use the same configurations other wise you have to change the core patterns in django.

The pattern for templates in django are like this:

polls/templates/polls/index.html

But to use it you have to add the installed app at the configs:

INSTALLED_APPS = [
'polls.apps.PollsConfig', #<-- Here this shoud be solve it
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',]

For more information look at:

https://docs.djangoproject.com/en/3.0/intro/tutorial02/#activating-models

My mistake was simply bad positioning:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'histories',
]

My solution was :

INSTALLED_APPS = [
'histories',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

This will surely solve the issue, instead of all the above try adding only below line in settings.py :

TEMPLATE_DIRS = (
    "appname/templates",
)

http://docs.djangoproject.com/en/1.2/ref/templates/api/#loading-templates Small fix for @zsquare answer:

import os
DIRNAME = os.path.abspath(os.path.dirname(__file__))

TEMPLATE_DIRS = ( DIRNAME+'/site-templates/' )

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