2017-12-27 922 views
0

我正在关注用于部署我的应用程序的django和mozilla教程。 我设置了一个虚拟env和基本设置,本地设置,等等,当我跑的命令:本地开发期间'TemplateDoesNotExist at /'错误

./manage.py runserver 0:8000 --settings=Books.settings.local

浏览器显示错误,模板家里没有发现...... 我觉得可能是由于env的一些问题,我不确定。

base.py

import os 


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


# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '2z9y' 
# Simplified static file serving. 
# https://warehouse.python.org/project/whitenoise/ 
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' 


ALLOWED_HOSTS = ['127.0.0.1', 'localhost','https://hackingonlineeducation.herokuapp.com'] 


# Application definition 

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    #third party apps 
    'star_ratings', 
    'crispy_forms', 
    #my_apps 
    'newsletter', 
] 

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'whitenoise.middleware.WhiteNoiseMiddleware', 
    '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 = 'Books.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', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'Books.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 
    } 
} 


# Password validation 
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators 

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.11/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.11/howto/static-files/ 

STATIC_URL = '/static/' 
STATICFILES_DIRS = [ 
    os.path.join(BASE_DIR, "static"), 
    os.path.join(BASE_DIR,"static","bootstrap") 
    # '/var/www/static/', 
] 
STATIC_ROOT=os.path.join(BASE_DIR,'static','static_root') 

MEDIA_URL='/media/' 
MEDIA_ROOT=os.path.join(BASE_DIR,'media','profile_images') 

#crispy_forms 
CRISPY_TEMPLATE_PACK = 'bootstrap3' 

LOGIN_REDIRECT_URL='profile' 
LOGOUT_REDIRECT_URL='home' 

local.py

from Books.settings.base import * 

DEBUG = True 
INSTALLED_APPS += (
    'debug_toolbar', # and other apps for local development 
) 

MIDDLEWARE +=[ 
'debug_toolbar.middleware.DebugToolbarMiddleware' 
] 

误差在浏览器中:

Exception Location:  /home/user/usr/lib/python3.6/site-packages/django/template/loader.py in get_template, line 25 
Python Executable: /home/user/usr/bin/python 
Python Version:  3.6.1 
Python Path:  

['/home/user/workspace/Books', 
'/home/user/usr/lib/python36.zip', 
'/home/user/usr/lib/python3.6', 
'/home/user/usr/lib/python3.6/lib-dynload', 
'/home/user/usr/lib/python3.6/site-packages', 
'/home/user/usr/lib/python3.6/site-packages/Sphinx-1.5.6-py3.6.egg', 
'/home/user/workspace/speakerfight/src/django-queryset-csv', 
'/home/user/workspace/speakerfight/src/django-activelink'] 

Template-loader postmortem 

Django tried loading these templates, in this order: 

Using engine django: 

django.template.loaders.filesystem.Loader: /home/user/workspace/Books/Books/templates/home.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /home/user/workspace/Books/venv/lib/python3.6/site-packages/django/contrib/admin/templates/home.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /home/user/workspace/Books/venv/lib/python3.6/site-packages/django/contrib/auth/templates/home.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /home/user/workspace/Books/venv/lib/python3.6/site-packages/star_ratings/templates/home.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /home/user/workspace/Books/venv/lib/python3.6/site-packages/crispy_forms/templates/home.html (Source does not exist) 
django.template.loaders.app_directories.Loader: /home/user/workspace/Books/venv/lib/python3.6/site-packages/debug_toolbar/templates/home.html (Source does not exist) 

另外我有更新manage.py如下:

import os 
import sys 

if __name__ == "__main__": 
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Books.settings.local") 
+0

在错误页它说的模板没有被发现,应该有(进一步向下)搜索到的目录列表以查找模板。这应该在标题为“Template-loader postmortem”的部分下,[见这里](https://docs.djangoproject.com/en/2.0/topics/templates/#template-postmortem)。你可以请张贴那些说什么? – ubadub

+1

你的错误回溯明确统计'/ home/user/workspace/Books/Books/templates/home.html' doublecheck如果文件存在及其权限 – Gahan

+0

@ubadub和@Gahan谢谢你的回答!我只注意到模板加载器正在从'/ home/user/workspace/Books/Books/templates/home.html'加载模板,它应该是'/ home/user/workspace/Books/templates/home.html',我很困惑,BASE_DIR似乎没问题? –

回答

0

这条线:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath则(__ FILE__)))

指定BASE_DIR如父母您的base.py所在的当前目录的目录。这意味着BASE_DIR将把/home/user/workspace/Books/Books/

你应该试试这个:

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__)) 
BASE_DIR = os.path.dirname(PROJECT_ROOT) 

,然后在TEMPLATES:

TEMPLATES = [ 
    { 
     ... 
     'DIRS': [os.path.join(PROJECT_ROOT,'templates')], 
     ... 
] 
+0

谢谢。我删除了不同的设置文件,并添加了这个:'DEBUG = bool(os.environ.get('DJANGO_DEBUG',True))''。它现在正在工作。 –