我知道这些问题已经有很多,但我还没有找到答案。我试图让我的主页加载,但我收到了这个错误:
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
让我感到困惑的是,如果我检查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/'
答案 0 :(得分:4)
您应该将文件settings.py TEMPLATE_DIR更改为TEMPLATE_DIRS。