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

时间:2015-02-25 03:02:49

标签: python django templates

因此,当我尝试加载模板时,我得到的模板不存在错误。以下是它告诉我它正在查看的目录:

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确实存在。

这是我的设置:

  • OSX 10.10.1
  • Python:2.7.6
  • Django:1.7.4

这是我的代码:

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变量,我不得不把它放在自己身上。

1 个答案:

答案 0 :(得分:1)

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

1.将模板文件夹放在投注应用程序中。

2.将statgrabber应用添加到已安装的应用列表中。