TemplateDoesNotExist in Heroku using Django Rest Framework



我安装了 djangorestframework 来在 Heroku 中打开一个休息服务。

当我在本地环境中打开网站时,我收到以下消息:

TemplateDoesNotExist at /
rest_framework/api.html
Request Method:     GET
Request URL:    http://192.168.56.102:8000/
Django Version:     1.9.2
Exception Type:     TemplateDoesNotExist
Exception Value:    
rest_framework/api.html
Exception Location:     /home/_ws_/rest/gps2/venv/lib/python2.7/site-packages/django/template/loader.py in get_template, line 43
Python Executable:  /home/_ws_/rest/gps2/venv/bin/python
Python Version:     2.7.5
Python Path:    
['/home/_ws_/rest/gps2',
 '/home/_ws_/rest/gps2/venv/lib64/python27.zip',
 '/home/_ws_/rest/gps2/venv/lib64/python2.7',
 '/home/_ws_/rest/gps2/venv/lib64/python2.7/plat-linux2',
 '/home/_ws_/rest/gps2/venv/lib64/python2.7/lib-tk',
 '/home/_ws_/rest/gps2/venv/lib64/python2.7/lib-old',
 '/home/_ws_/rest/gps2/venv/lib64/python2.7/lib-dynload',
 '/usr/lib64/python2.7',
 '/usr/lib/python2.7',
 '/home/_ws_/rest/gps2/venv/lib/python2.7/site-packages']
Server time:    Fri, 22 Apr 2016 14:53:25 +0000

我该怎么办?这可能与 settings.py 文件有关。

我的是官方的,对数据库进行了移动(在本例中为空)并添加了一个轨道项目。

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

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ""
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
# Application definition
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tracks',   
)
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',
    'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'gps2.urls'
TEMPLATES = (
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates"),],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'debug': DEBUG,
        },
    },
)
WSGI_APPLICATION = 'gps2.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '',
        'USER': '',
        'HOST': '',
        'PASSWORD': '',
    }
}
AUTH_PASSWORD_VALIDATORS = (
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
)
# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Update database configuration with $DATABASE_URL.
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, 'static'),
]
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

我也有消息:

Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
    django.template.loaders.filesystem.Loader: /home/_ws_/rest/gps2/templates/rest_framework/api.html (Source does not exist)
    django.template.loaders.app_directories.Loader: /home/_ws_/rest/gps2/venv/lib/python2.7/site-packages/django/contrib/admin/templates/rest_framework/api.html (Source does not exist)
    django.template.loaders.app_directories.Loader: /home/_ws_/rest/gps2/venv/lib/python2.7/site-packages/django/contrib/auth/templates/rest_framework/api.html (Source does not exist)

该文件实际上位于:

/home/_ws_/rest/gps2/venv/lib/python2.7/site-packages/rest_framework/templates/rest_framework/api.html

如何获取此目录?

您尚未将"rest_framework"添加到INSTALLED_APPS设置中,如安装文档中所述。

没有它,Django 将不会在 DRF 目录中查找相关模板。

最新更新