Django 找不到模板,文件存在并指定了 TEMPLATE_DIR
Posted
技术标签:
【中文标题】Django 找不到模板,文件存在并指定了 TEMPLATE_DIR【英文标题】:Django can't find templates, file exists and specified TEMPLATE_DIR 【发布时间】:2015-03-09 01:28:05 【问题描述】:我知道已经有很多这样的问题,但我一直无法找到答案。我正在尝试加载我的主页,但我收到了这个错误:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.7.1
Exception Type: TemplateDoesNotExist
Exception Value:
base.html
Exception Location: /Users/user/.virtualenvs/screen_savers/lib/python3.4/site-packages/django/template/loader.py in find_template, line 136
Python Executable: /Users/user/.virtualenvs/screen_savers/bin/python
Python Version: 3.4.1
Python Path:
['/Users/user/Devspace/scren_savers/the_screen_savers',
'/Users/user/.virtualenvs/screen_savers/lib/python34.zip',
'/Users/user/.virtualenvs/screen_savers/lib/python3.4',
'/Users/user/.virtualenvs/screen_savers/lib/python3.4/plat-darwin',
'/Users/user/.virtualenvs/screen_savers/lib/python3.4/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4',
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin',
'/Users/user/.virtualenvs/screen_savers/lib/python3.4/site-packages']
而且似乎无法弄清楚原因。我在我的 settings.py 中正确指定了我的TEMPLATE_DIR
并确保该文件存在于它所说的位置。我还查看了所有这些问题,人们有同样的问题,但没有一个能解决我的问题:
Django App template Loader, can't find app templates
Django template Path
Django can't find template
Django TemplateDoesNotExist?
Template does not exist
Django cant find templates
真正让我困惑的是,如果我检查 BASE_DIR 和 TEMPLATE_DIR 的值,它会给我:
/Users/user/Devspace/scren_savers/the_screen_savers
/Users/user/Devspace/scren_savers/the_screen_savers/templates
这正是我期望看到的。它似乎知道模板在哪里,但仍然看不到它们。任何帮助将不胜感激。
还有一个额外的、不太重要的问题:
我已经看到 BASE_DIR + '/templates'
和 os.path.join(BASE_DIR, "templates")
建议作为指定 TEMPLATE_DIR
的正确方法。其中一个比另一个更好(如果是,为什么)还是这只是个人喜好问题?
这是我要加载的视图:
from django.http import HttpResponse
from django.shortcuts import render
from django.views.generic import View
class home_page(View):
def get(self, request):
return render(request, 'base.html')
这是我的设置文件:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIR = (
BASE_DIR + "/templates/",
)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'screen_savers',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'screen_savers.urls'
WSGI_APPLICATION = 'screen_savers.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/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.6/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.6/howto/static-files/
STATIC_URL = '/static/'
【问题讨论】:
【参考方案1】:您应该将文件 settings.py TEMPLATE_DIR 更改为 TEMPLATE_DIRS。
【讨论】:
以上是关于Django 找不到模板,文件存在并指定了 TEMPLATE_DIR的主要内容,如果未能解决你的问题,请参考以下文章