2017-12-27 463 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))''。它現在正在工作。 –