TemplateDoesNotExist 错误 Django,它正在寻找正确的目录

TemplateDoesNotExist Error Django, it is looking in the right directory

所以我在尝试加载模板时收到模板不存在的错误。这是它告诉我它正在查找的目录:

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/Users/vmgarcia/PycharmProjects/statgrabber/statgrabber/templates/betting/index.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/Users/vmgarcia/PycharmProjects/statgrabber/lib/python2.7/site-packages/django/contrib/admin/templates/betting/index.html (File does not exist)
/Users/vmgarcia/PycharmProjects/statgrabber/lib/python2.7/site-packages/django/contrib/auth/templates/betting/index.html (File does not exist)

它查找的第一个路径是正确的路径,文件 index.html 确实存在。

这是我的设置:

这是我的代码:

settings.py

"""
Django settings for statgrabber project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2hp-5k(@#3u5!o^%r-&i(flq-3a@85ubpy%kcq&p05z(_j+=67'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Project templates
# TEMPLATES = 
TEMPLATE_LOADERS = ('django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader')   
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),)
# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'betting',

)
# /statgrabber/statgrabber/templates/betting/index.html
# /Users/vmgarcia/PycharmProjects/statgrabber

MIDDLEWARE_CLASSES = (
    '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',
)

ROOT_URLCONF = 'statgrabber.urls'

WSGI_APPLICATION = 'statgrabber.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

views.py

from django.shortcuts import render
from django.http import HttpResponse

# site index
def index(request):
    context_dict = {'boldmessage': 'I am bold from the context'}
    return render(request, 'betting/index.html', context_dict)

urls.py

from django.conf.urls import patterns, url
from betting import views

urlpatterns = patterns('', 
    url(r'^$', views.index, name='index'))

如有任何帮助,我们将不胜感激。我花了相当多的时间试图找出问题所在,但我无法弄清楚。

需要注意的是 django 不会自动生成 TEMPLATE_DIRS 和 TEMPLATE_LOADERS 变量,我必须自己将其放入。

我看到的问题是您没有将 statgrabber 应用程序添加到已安装应用程序列表中,并且 django 模板引擎无法访问里面的模板 that.I 可以想到两个解决方案。

1.Either 将您的模板文件夹放入投注应用程序中。

2.Add statgrabber 应用到已安装应用列表。