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

标签 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

这是我的代码:

设置.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/'

View .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)

url.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 模板引擎无法访问其中的模板。我可以想到两种解决方案。

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

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

关于python - TemplateDoesNotExist 错误 Django,它正在寻找正确的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28710368/

相关文章:

python - 使用 Signals 在 django 中注册用户后如何创建用户配置文件?

ruby-on-rails - 让用户为自定义布局编写数据库存储的 CSS

c++ - 类模板成员函数特化

python - 使用 Pandas 将某个 df 写入下一个空列

python - 在列表中查找属性等于某个值的对象(满足任何条件)

python - Django URL 正则表达式 "is not a valid regular expression"错误

python - API View 中的 Django Rest 框架更新方法以更改 db json 中的值

python - 如何检查 Paramiko 是否成功将文件上传到 SFTP 服务器?

python - 使用格式为 PDF/X-1a 的 Python 将 HTML 转换为 PDF

c++ - 使用继承时未解析的引用